-
Membership testing is the focus of program design, and the set of valid input values is an example. When the user enters the content in the collection, his input is valid or invalid.
Simple membership tests recommend using sets.
valid_input={"yes","y","no","n"}
answer=None
while answer not in valid_inputs:
answer=input("Continue? [y,n]").lower()
A set keeps elements unchanged in a state in no particular order. If an element is a member of a set, then no further elements can be added to the set.
valid_inputs={"yes","y","no","n"}
valid_inputs.add("y")
valid_inputs
{'no','y','n','yes'}
We created a set with 4 different strings - valid_inputs. You cannot add another y to a set that already contains y. The content of the set cannot be changed.
Note that the order of the set elements is not exactly the order we first provided. A set cannot maintain any particular order of elements, it can only determine whether an element exists in the set.
month_name_list=["jan","Feb","Mar","Apr",
"May","Jun","Jul","Aug",
"Sep","Oct","Nov","Dec"]
month_name_list[8]
"sep"
month_name_list.index("Feb")
1
We created a list with 12 string elements - month_name_list. An element can be selected by providing its position, and the index() method can be used to find the index of the element in the list.
The position of lists in Python always starts at 0, and the same is true for tuples and strings.
If the number of elements in the set is fixed, e.g. three values for an RGB color, then it may be necessary to use a tuple instead of a list. If the number of elements will grow and change, then a list is a better choice than a tuple.
scheme={"Crimson":(220,14,60),
"DarkCyan",(0,139,139),
"Yellow":(255,255,00)}
scheme['Crimson']
(220,14,60)
We added a mapping from color names to RGB color tuples in the dictionary scheme. When using a key, such as "Crimson", the value bound to that key can be retrieved.
Instead of creating a set with lists as elements, we can convert each list to a tuple. We can create sets with immutable tuples as elements.
Similarly, dictionary keys must be immutable. You can use numbers, strings, or tuples as dictionary keys, but not lists, sets, or other mutable maps as dictionary sets.
Each of Python's built-in collections provides a unique set of functions. These collections also provide a lot of overlapping functionality, which can be a challenge for Python newbies.
In fact, the collections.abc module provides a kind of guide through built-in collections. The collections.abc module defines an abstract base class (ABC) that supports concrete classes. We'll see what these constructs do by the names in this set of definitions.
Collections can be divided into 6 categories according to abstract base classes.
-
Mutable set: set collection. -
Immutable sets: frozenset collections.
-
Mutable sequence: list collection. -
Immutable sequences: collections of tuples.
-
mutable map: dict collection. -
Immutable Maps: There are no built-in frozen maps.
Python's libraries provide numerous additional implementations of these core collection types.
-
namedetuple: Tuple. Each element in the tuple can be given a name. Using rgb_color.red is slightly cleaner than rgb_color[0]. -
deque: Double-ended queue. This is a mutable sequence with the added optimization of pushing and popping from each end. Lists achieve similar functionality, but deques are more efficient. -
defaultdict: dictionary. Default values can be provided for missing keys. -
Counter: Dictionary. Can be used to count the number of occurrences of a key. Sometimes called a multiset or bag. -
OrdereDict: Dictionary. The order in which keys were created is preserved. -
ChainMap: Dictionary. Combine multiple dictionaries into a single map.
-
Array: Python's list structure is a typical array and provides similar performance to a linked list of arrays. -
Tree: Tree structures can be used to create sets, ordered lists, or key-value maps, and trees can be viewed as an implementation technique rather than a data structure with a unique set of features. -
Hash: Python uses hashes to implement dictionaries and sets, which are fast but memory-intensive. -
graph: Python does not have a built-in graph data structure. But the graph structure can easily be represented as a dictionary, where each node has a list of adjacent nodes.