When should I use a dictionary, list or set?
Are there scenarios that are more suited for each data type?
When should I use a dictionary, list or set?
Are there scenarios that are more suited for each data type?
In short, use:
list - if you require an ordered sequence of items.
dict - if you require to relate values with keys
set - if you require to keep unique elements.
A list is a mutable sequence, typically used to store collections of homogeneous items.
A list implements all of the common sequence operations:
x in l and x not in ll[i], l[i:j], l[i:j:k]len(l), min(l), max(l)l.count(x)l.index(x[, i[, j]]) - index of the 1st occurrence of x in l (at or after i and before j indeces)A list also implements all of the mutable sequence operations:
l[i] = x - item i of l is replaced by xl[i:j] = t - slice of l from i to j is replaced by the contents of the iterable tdel l[i:j] - same as l[i:j] = []l[i:j:k] = t - the elements of l[i:j:k] are replaced by those of tdel l[i:j:k] - removes the elements of s[i:j:k] from the listl.append(x) - appends x to the end of the sequence l.clear() - removes all items from l (same as del l[:])l.copy() - creates a shallow copy of l (same as l[:])l.extend(t) or l += t - extends l with the contents of tl *= n - updates l with its contents repeated n timesl.insert(i, x) - inserts x into l at the index given by il.pop([i]) - retrieves the item at i and also removes it from ll.remove(x) - remove the first item from l where l[i] is equal to xl.reverse() - reverses the items of l in placeA list could be used as stack by taking advantage of the methods append and pop.
A dictionary maps hashable values to arbitrary objects. A dictionary is a mutable object. The main operations on a dictionary are storing a value with some key and extracting the value given the key.
In a dictionary, you cannot use as keys values that are not hashable, that is, values containing lists, dictionaries or other mutable types.
A set is an unordered collection of distinct hashable objects. A set is commonly used to include membership testing, removing duplicates from a sequence, and computing mathematical operations such as intersection, union, difference, and symmetric difference.
For C++ I was always having this flow chart in mind: In which scenario do I use a particular STL container?, so I was curious if something similar is available for Python3 as well, but I had no luck.
What you need to keep in mind for Python is: There is no single Python standard as for C++. Hence there might be huge differences for different Python interpreters (e.g. CPython, PyPy). The following flow chart is for CPython.
Additionally I found no good way to incorporate the following data structures into the diagram: bytes, byte arrays, tuples, named_tuples, ChainMap, Counter, and arrays.
OrderedDict and deque are available via collections module.heapq is available from the heapq moduleLifoQueue, Queue, and PriorityQueue are available via the queue module which is designed for concurrent (threads) access. (There is also a multiprocessing.Queue available but I don't know the differences to queue.Queue but would assume that it should be used when concurrent access from processes is needed.)dict, set, frozen_set, and list are builtin of courseFor anyone I would be grateful if you could improve this answer and provide a better diagram in every aspect. Feel free and welcome.

PS: the diagram has been made with yed. The graphml file is here
In combination with lists, dicts and sets, there are also another interesting python objects, OrderedDicts.
Ordered dictionaries are just like regular dictionaries but they remember the order that items were inserted. When iterating over an ordered dictionary, the items are returned in the order their keys were first added.
OrderedDicts could be useful when you need to preserve the order of the keys, for example working with documents: It's common to need the vector representation of all terms in a document. So using OrderedDicts you can efficiently verify if a term has been read before, add terms, extract terms, and after all the manipulations you can extract the ordered vector representation of them.
Dictionary: A python dictionary is used like a hash table with key as index and object as value.
List: A list is used for holding objects in an array indexed by position of that object in the array.
Set: A set is a collection with functions that can tell if an object is present or not present in the set.
May be off topic in terms of the question OP asked-
To compare them visually, at a glance, see the image-
Dictionary: When you want to look up something using something else than indexes. Example:
dictionary_of_transport = {
"cars": 8,
"boats": 2,
"planes": 0
}
print("I have the following amount of planes:")
print(dictionary_of_transport["planes"])
#Output: 0
List and sets: When you want to add and remove values.
Lists: To look up values using indexes
Sets: To have values stored, but you cannot access them using anything.