- Lists can have duplicate items but sets can’t.
- You can update an item in a list but not in a tuple.
- You can get the third item from a tuple but not from a set.
These are just a few things to know about Python Data Structures. There are surely reasons behind these differences, which make each one suitable for a particular task.
To write efficient Python scripts, one must know the key characteristics of these data structures as well as how to use them.
For instance, we can use sets to find the different items between two lists:
lst_1 = ["Jane", "Emily", "John", "Max", "Emily", "Jane", "Matt"]
lst_2 = ["Jane", "Matt", "John", "Jane", "Emily"]
# convert each list to a set, find the difference using subtraction
difference = list(set(lst_1) - set(lst_2))
print(difference)
# output
['Max']
We’ll learn three must-know concepts about Python‘s four built-in data structures: dictionary, set, list, tuple.
1. Mutable
If a data structure is mutable, we can update its items or add new items to it.
- List: mutable (we can add new items to a set and update existing ones)
- Set: mutable (we can add new items to a set)
- Dictionary: mutable (we can add new items to a set and update existing ones)
- Tuple: not mutable
Let’s do a few examples to show how to add new items to a list, a set, and a dictionary.
# set
myset = set([1, 2, 3])
myset.add(4)
print(myset)
# output
{1, 2, 3, 4}
# list
mylist = [1, 2, 3]
mylist.append(4)
print(mylist)
# output
[1, 2, 3, 4]
# dictionary
mydict = {"John": 24, "Jane": 26}
mydict["Emily"] = 30
print(mydict)
# output
{'John': 24, 'Jane': 26, 'Emily': 30}
We can add items to a tuple but it does not modify the tuple itself. Instead, we get a new tuple with new items added.
# tuple
mytuple = (1, 2, 3)
newtuple = mytuple + (4,)
print(newtuple)
# output
(1, 2, 3, 4)
In the example above, the variable mytuple
remains the same.
2. Ordered
The items in lists and tuples are ordered. They’re assigned an index, which can be used for accessing a specific item (e.g. first item, fifth item) or a sequence of items (e.g. between the fifth and tenth items).
On the other hand, set and dictionary items are not ordered so we can’t really talk about an index.
Note: Starting from Python 3.1, we also have OrderedDict , which is a subclass of dictionary that has methods specialized for rearranging dictionary order.
Here are a few examples to show how indexing can be used with lists.
mylist = [11, 14, 13, 12, 10, 16, 22]
# first item
mylist[0] # output 11
# third item
mylist[2] # output 13
# first three items
mylist[:3] # output [11, 14, 13]
# last item
mylist[-1] # output 22
# every other item
mylist[::2] # output [11, 13, 10, 22]
The examples above work for tuples as well so there is no need to do separate examples for them.
3. Uniqueness
We can’t have duplicate items in some data structures such as sets. This can be useful for a variety of tasks. We also need data structures that allow for duplicate items for some tasks.
- Sets do not contain duplicate items
- Tuples and lists can contain duplicate items
- Dictionaries can contain duplicate values but not duplicate keys
If we create a set from a string, it’ll only have the unique characters in the string:
myset = set("Python is awesome!")
print(myset)
# output
{'y', 'o', ' ', '!', 'h', 'a', 'n', 't', 'P', 'e', 'i', 'w', 's', 'm'}
A dictionary is an unordered collection of key-value pairs. The keys can be considered as the index (or address) and they can be used for accessing values. Thus, we can’t have duplicate keys in a dictionary.
mydict = {"Jane": 25, "Adam": 30, "Jane": 21}
print(mydict)
# output
{'Jane': 21, 'Adam': 30}
We tried to add two "Jane" keys but the resulting dictionary only has one.
If you try to add a key that already exists in the dictionary, its value will be updated with the new value.
print(mydict)
# output
{'Jane': 21, 'Adam': 30}
mydict["Adam"] = 45
print(mydict)
# output
{'Jane': 21, 'Adam': 45}
Final words
When I first heard of data structures, I thought only data-based roles need to know about them. If you’re not a data scientist or data analyst, you should not worry too much about them.
It did not take me long to realize the importance of data structures not only for data-based roles but also for software developers and engineers. It’s of crucial importance to have a comprehensive understanding of data structures in order to write efficient and robust programs.
In this article, we focused on three key concepts that can be considered as a differentiating factor for data structures.
I also wrote separate articles for each data structure if you’d like to learn more about them:
You can become a Medium member to unlock full access to my writing, plus the rest of Medium. If you already are, don’t forget to subscribe if you’d like to get an email whenever I publish a new article.
Thank you for reading. Please let me know if you have any feedback.