Python has a wealth of built-in data structures, and many applications are built from built-in data structures. These collections cover a variety of situations. The built-in tuple and string structures are different from list structures. There are some important similarities between them, but there are also some differences. These built-in data structures help us work with data collections. It is difficult to determine which data structure is suitable for a certain scenario.

How to choose which structure to use? What is the function of lists, sets and dictionaries? Why do tuples and frozen collections occur?


  1. 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=Nonewhile 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.


2. Is it possible to represent elements based on their position in the collection? A relevant example is the line in the input file, where the line number is the position of the line in the collection.
When an element must be identified by index or position, a list must be used.
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.


3. Is it possible to identify an element in a collection by its key instead of its position? Examples might include a mapping between strings (i.e. words) and integers representing the frequencies of those words, or a mapping between a color name and an RGB tuple for that color.
Some kind of mapping is recommended when a non-position key must be used to identify an element. The built-in mapping is a dictionary (dict). More features can be added through extensions.
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.

4. Carefully consider the elements in the set and the variability of the keys in the dictionary. Each element in the set must be an immutable object. Numbers, strings, and tuples are immutable and can be collected into sets. Since lists, dictionaries, or sets are mutable, they cannot be used as elements of sets. For example, it is not possible to construct a set with lists as elements.

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.

Sets: A unique feature is element membership testing. Sets cannot hold duplicate elements.
  • Mutable set: set collection.
  • Immutable sets: frozenset collections.
Sequences: Unique feature is to provide index positions for elements.
  • Mutable sequence: list collection.
  • Immutable sequences: collections of tuples.
Maps: The unique feature is that each element has a key that points to a value.
  • 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.


The collections module contains many variants of the built-in collections.
  • 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.
There are many more similar constructs in the Python standard library. We can define a priority queue using the heapq module. The bisect module contains methods for very fast searching of sorted lists, which can be used with a performance closer to that of a quick dictionary lookup.
  • 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.
Almost any type of data structure can be implemented in Python, either the built-in structure has the corresponding essential characteristics, or you can find a built-in structure that can be used in front of you.