In Python, a data structure is a data type that can hold information as a group. This includes lists, dictionaries, and tuples. It is a misconception that types such as structs and classes are considered data structures. These two types are entities of object-oriented programming and do not hold data themselves; however, they can hold data structures that do store the data. This post will break down each structure, describe when to use it, and provide code examples of each. Click on each heading to check out official Python documentation if you want a more technical breakdown.
List
A list is a generic container of types that can be stored as a group. Lists are the most generic types and nearly universal in what they can store. Typical uses of this data structure are storing temporary data for access and storing unkeyed data. One thing to note about Python lists is that they can store multiple types in the same list; you do not need to store all strings or numbers. Lastly, it should be noted that lists, like other data structures, do not replicate when setting two lists equal to each other. If you set a list equal to another list, a pointer is created, and an operation, such as clearing a list, will perform the action on both lists. In short, do not set one list equal to another. You can use tuples instead, which are immutable. A code example of lists and how to loop through them can be found below. The following code will use two populated lists to create a deck of cards and store it in an empty list.
cardDeck = []
values = ["A","K","Q","J","10","9","8","7","6","5","4","3","2"]
suites = ["H","C","D","S"]
for v in values:
for s in suites:
cardDeck.append(v+s)
print(cardDeck)
Dictionaries
In short, a dictionary is a data structure that operates as an indexed list. Data within a dictionary is stored as a key-value pair. Remember that, although the data is indexed, dictionaries are not sortable. This means that it is best practice not to display a dictionary directly but to refer to its keys directly. Another difference between a dictionary and a list is that a dictionary is defined using {} while lists use []. Below is an example of a dictionary and how it may be used to keep track of the grades of academic students.
students = {"John Doe": 88, "Jane Doe": 92, "Bobby Doe": 65}
print(students["Jane Doe"])
print(students["John Doe"])
print(students["Bobby Doe"])
Tuples
If we could sum up tuples in the shortest way possible, we’d say that tuples are just immutable lists. Immutable means that data cannot be changed once initialized. Tuples are great for data that needs integrity, such as survey responses. The official Python documentation mentions that this data structure is also used when mixed data types are needed, but real-world examples show that lists can, and often do, contain more than one data type. Below provides an example of tuples for a survey answer being transferred from a list to a tuple. We also show an example of what happens when you set one list equal to another and empty one. The result is the officialresponses remaining unchanged but both intialresponses and copyofresponses are eventually wiped out.
intialresponses = ["agree", "satisfied", 10, "always", "yes", "no", 8]
copyofresponses = intialresponses
officialresponses = tuple(intialresponses)
print(officialresponses)
print(copyofresponses)
intialresponses.append("lol")
print(officialresponses)
print(copyofresponses)
#clearing intialresponses will wipe out the copy because the copy is just a pointer to the original
intialresponses.clear()
print(officialresponses)
print(copyofresponses)