Andrei Dumitrescu

In this article, we’ll discuss the built-in Python data structures, which are: list, tuple, set, frozenset, and dictionary.

Python is one of the most popular programming languages in the world, being used for web development, data science, robotics, machine learning (ML), artificial intelligence (AI), internet of things (IoT), or network automation. 

Programming code displayed on a monitor.

Any application works with data that needs organization, management, and storage in a data structure for easy and efficient access.

There are five built-in Python data structures, each of them useful at solving a particular problem or task.

Note that all the examples in this article run in Python IDLE.

Master Python Programming: The Complete Python Bootcamp 2022

Last Updated October 2021

  • 357 lectures
  • All Levels
4.6 (917)

Python for Beginners, 100% Hands-on, Hundreds of Python Coding Challenges and Quizzes, Complete Python E-Book. | By Andrei Dumitrescu, Crystal Mind Academy

Explore Course

1. List

A list is a mutable, ordered sequence or collection of items. It can hold any object type, such as integers, floats, strings, other lists, tuples, dictionaries, and so on.

To create a new empty list, you use either a pair of square brackets ([ ]) or the list() constructor.

>>> my_list = [] # creating an empty list
>>> type(my_list)
<class 'list'>
>>> my_list = list() # creating an empty list
>>> type(my_list)
<class 'list'>

To create a list that stores data, use a pair of square brackets ([ ]) and the elements of the list with a comma between them.

>>> my_list = [1, 2.5, "Python", 10]

Lists like strings support indexing and slicing. We can refer to any item in the list by using its index number, which starts from zero.

>>> my_list[1]
2.5

Or, you can slice a list using a starting and an ending index. The starting index is included, but the ending one is not.

>>> my_list[1:3]
[2.5, 'Python']

Lists are also mutable, meaning you can alter them. You can change a specific element of the list, add a new element to the list, or remove an existing element from the list.  

>>> my_list[2] = "Go"  # changing the element at index 2
>>> my_list.append(33) # appending a new element to the list
>>> my_list.remove(1)  # removing the first occurrence of the element

There are also functions attached to lists called methods.

To display all methods available, call the dir() function and give it a list as an argument.

>>> dir(my_list)  # listing all methods

Note that equality operator ( ==) creates a reference to the same list, and not a copy of it. 

>>> l1 = my_list # creating a reference
>>> l2 = my_list.copy() # creating a copy
>>> l1 is my_list
True
>>> l2 is my_list
False

2. Tuple

Tuples are immutable lists, meaning they are unchangeable.

The best practice is to store in a data structure of type tuple the elements that you know will not change. Some examples of such elements are the days of the week, the months of the year, the GPS coordinates of a specific location, etc.

You declare a tuple using parenthesis instead of square brackets.

>>> t1 = () # creating an empty tuple
>>> t2 = tuple() # creating an empty tuple
>>> type(t1), type(t2)
(<class 'tuple'>, <class 'tuple'>)

Indexing and slicing are applicable to tuples too.

All the methods that altered the lists are not available to tuples.  You can list all the methods available by calling dir() with a tuple object as the argument.

3. Set 

Sets are unordered collections of unique elements. A set is not a sequence type in Python.

In the real world, there are many unordered collections without duplicates. Let’s take just a few examples: social security numbers, email addresses, IP and MAC addresses, and so on. All these are collections of unique and unordered elements. Duplicates are not allowed, and order doesn’t matter!

If you want to have such collections in your applications, it’s a good idea to save them as sets.

The Python implementation of the set data structures uses a hashtable as its underlying data structure. This explains the O(1) membership checking, since looking up an item in a hashtable is an O(1) operation, on average. It does a direct lookup to access an element.

The disadvantage of sets compared to lists and tuples is that they use more memory. So there is a trade-off between speed and memory.

To create an empty set call the set() constructor. Note that a pair or curly braces {} creates a dictionary, not a set.

>>> s1 = set() # creating an empty set
>>> type(s1)
<class 'set'>

To create a set that contains elements, use {} and comma between the elements.

>>> s2 = {1, 2, 3, 1, 2, 2, 1}
>>> s2
{1, 2, 3}   # => the duplicates are automatically removed

You cannot access the elements of a set by index because there is no order.

>>> s2[0]
Traceback (most recent call last):
  File "/usr/lib/python3.8/idlelib/run.py", line 559, in runcode
    exec(code, self.locals)
  File "<pyshell#82>", line 1, in <module>
TypeError: 'set' object is not subscriptable

The elements of a set can be of different types but must be immutable. Immutable data types that cannot be stored in a set are list, set or dictionary.

>>> s3 = {1, 'a', (1, 2, 3)}  # OK, all elements are immutable
>>> s3 = {1, 'a', [1, 2, 3]}  # Error, list is not immutable

add() adds an element to the set, and remove() removes the given element from the set. 

Sets are mutable, so elements can be added or removed from the set.

>>> s3.add('python')
>>> s3
{1, 'a', 'python', (1, 2, 3)}
>>> s3.remove('a')
>>> s3
{1, 'python', (1, 2, 3)}

If the element does not belong to the set, remove() returns an error.

>>> s3.remove('a')  #=> error, ‘a’ was already removed

Another method that removes an element from the set is discard().

The difference between remove() and discard() is that discard() does not return an error if the element to be removed is not part of the set.

>>> s3.discard('Go')  # no error even though 'Go' is no part the of the set

To see a list with all the methods of a set object, call dir() with a set as an argument.

The Python implementation of set supports many operations such as Difference (-), Symmetric difference(^), Union (|), Intersection (&)

Note that two sets are disjoint sets if they have no common elements.

4. Frozenset

Frozensets are nothing more than immutable sets. They have the same properties and behavior as sets, except that they cannot be mutated or modified. This means that the mutation operations we use in sets like add(), update(), and so on will NOT work with frozensets.

Being immutable, frozensets work as keys in dictionaries or as elements in another set or frozenset, and this can be useful sometimes.

To create a frozenset, you use the frozenset() function or you create the frozenset from another iterable object like a string, list, tuple, or set by passing the iterable as the argument to the frozenset() function.

>>> fs1 = frozenset()
>>> fs2 = frozenset([1, 2, 3, 3, 6])
>>> fs2
frozenset({1, 2, 3, 6})

5. Dictionary

Dictionaries are everywhere in Python. Modules, classes, objects, and even sets are all implemented as dictionaries. 

If you are used to other programming languages like JavaScript, Ruby, or Go, you can think of a dictionary as being just like an object in JS, a hash in Ruby, or a map in Go.

In Python, a dictionary is an unordered collection of key: value pairs separated by commas and enclosed by curly braces.

To create an empty dictionary, use a pair of curly braces {} or call the dict() constructor.

>>> dict1 = {}
>>> dict2 = dict()
>>> type(dict1)
<class 'dict'>
>>> type(dict2)
<class 'dict'>

>>> person = {'name':'John', 'age':30}
>>> person
{'name': 'John', 'age': 30}

A dictionary is a mutable object, meaning that we can add to or remove elements from the dictionary.

>>> person['city'] = 'London' # adding a key:value pair
>>> person
{'name': 'John', 'age': 30, 'city': 'London'}

>>> del person['age']  #  removing a key:value pair
>>> person
{'name': 'John', 'city': 'London'}

Values can be any Python object, but keys need to be hashable or immutable objects.

One important thing to know is that keys must also be unique. So keys are unique and immutable. Values, however, don’t have to be unique. Keep this in mind!

Extracting the corresponding value of a specified key is similar to accessing an element of a list, only that we don’t specify an index; we specify the associated key for the value we want to extract. Note that if the key doesn’t exist, it returns a KeyError.

>>> person['city']
'London'

There are three methods that deal with keys and values in a dictionary.

The first one is the keys() method. This method obtains a dictionary view, having the keys of the dictionary as elements. This view can be easily converted to a list using the list function.

>>> book = {'Author': 'Yuval Noah Harari', 'Title': 'Sapiens', 'ISBN': '978-0062316097'}
>>> book.keys()
dict_keys(['Author', 'Title', 'ISBN'])

A similar method is values(), which is used to get the values of the dictionary. Like the keys() method, it returns a dictionary view object having the values of the dictionary as elements. We can convert this object to a list using the list() function.

>>> book.values()
dict_values(['Yuval Noah Harari', 'Sapiens', '978-0062316097'])

The third method is items(). It returns a list of tuples, each tuple containing the key and the value of each dictionary pair.

>>> book.items()
dict_items([('Author', 'Yuval Noah Harari'), ('Title', 'Sapiens'), ('ISBN', '978-0062316097')])

One important remark is that the order of the keys, values, and items returned by these three methods is the same. That means that, for example, the 3rd key returned by the keys() method corresponds to the 3rd value returned by the values() method.

Also note that the views returned by these three methods are dynamic, reflecting any changes in the dictionary.

Now that we know how to get the keys and the values of a dictionary, let’s see how to iterate over a dictionary.

>>> for k in book.keys():
	print(f'key is {k}')
key is Author
key is Title
key is ISBN
>>> for k, v in book.items():
	print(f'key is {k}, and value is {v}')
key is Author, and value is Yuval Noah Harari
key is Title, and value is Sapiens
key is ISBN, and value is 	978-0062316097

Another method used to extend the dictionary with the items of another dictionary is update().

>>> d1 = {'pages': 443, 'Year': 2011}
>>> book.update(d1)
>>> book
{'Author': 'Yuval Noah Harari', 'Title': 'Sapiens', 'ISBN': '978-0062316097', 'pages': 443, 'Year': 2011}

One more word about order. Even if a dictionary is unordered and we cannot count on order, starting with Python 3.6, a dictionary remains ordered in the order of insertion.Now that you have a good understanding of the built-in data structures in Python, you can go ahead and read 5 Python Programming Examples for Everyday Life.

Page Last Updated: November 2021

Python students also learn

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy for Business.

Request a demo

Courses by Andrei Dumitrescu

Linux Administration: The Complete Linux Bootcamp for 2022
Andrei Dumitrescu, Crystal Mind Academy
4.6 (746)
Bestseller
Ethical Hacking and Network Security from Scratch in 2022
Andrei Dumitrescu, Crystal Mind Academy
4.5 (278)
Master Go (Golang) Programming:The Complete Go Bootcamp 2022
Andrei Dumitrescu, Crystal Mind Academy
4.5 (943)
Master Python Programming: The Complete Python Bootcamp 2022
Andrei Dumitrescu, Crystal Mind Academy
4.6 (917)
Master Network Automation with Python for Network Engineers
Andrei Dumitrescu, Crystal Mind Academy
4.7 (2,021)
Bestseller
Linux Security: The Complete Iptables Firewall Guide
Andrei Dumitrescu, Crystal Mind Academy
4.7 (757)
Master Ethereum & Solidity Programming From Scratch in 2022
Andrei Dumitrescu, Crystal Mind Academy
4.6 (1,346)
Linux Administration: Build 5 Hands-On Linux Projects 2022
Andrei Dumitrescu, Crystal Mind Academy
4.7 (131)

Courses by Andrei Dumitrescu