Data Structures in Python: An Introduction

Ingo Krause
4 min readJan 20, 2023

--

Photo by Hitesh Choudhary on Unsplash

In this tutorial, we will learn about the different data structures in Python, including lists, tuples, dictionaries, and sets and how to use them to store and manipulate data. We will also cover some advanced topics such as list comprehension and dictionary comprehension.

Lists

A list is a collection of items that are ordered and changeable. Lists are written with square brackets and elements are separated by commas. Here’s an example of how to create a list:

# Create a list 
my_list = [1, 2, 3, 4, 5]
print(my_list) # Output: [1, 2, 3, 4, 5]

You can access the elements of a list by using the index of the element. Python lists use zero-based indexing, which means the first element has an index of 0, the second element has an index of 1, and so on. Here’s an example of how to access the elements of a list:

# Create a list
my_list = [1, 2, 3, 4, 5]

# Access the first element
first_element = my_list[0]
print(first_element) # Output: 1

# Access the last element
last_element = my_list[-1]
print(last_element) # Output: 5

You can also use the slice notation to access multiple elements of a list. The slice notation uses the format start:stop:step to specify the range of elements to access. Here's an example of how to use the slice notation:

# Create a list
my_list = [1, 2, 3, 4, 5]

# Access the first three elements
first_three = my_list[0:3]
print(first_three) # Output: [1, 2, 3]

Tuples

A tuple is similar to a list, but it’s immutable, which means you can’t modify its elements after it’s created. Tuples are written with round brackets and elements are separated by commas. Here’s an example of how to create a tuple:

# Create a tuple
my_tuple = (1, 2, 3, 4, 5)
print(my_tuple) # Output: (1, 2, 3, 4, 5)

You can access the elements of a tuple in the same way as you would access the elements of a list, using the index or the slice notation. However, you can’t modify the elements of a tuple once it’s created.

Dictionaries

A dictionary is a collection of items that are unordered, changeable and indexed. Dictionaries are written with curly braces and elements are separated by commas. Each element has a key and a value. Here’s an example of how to create a dictionary:

# Create a dictionary
my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
print(my_dict) # Output: {'name': 'John', 'age': 30, 'city': 'New York'}

You can access the elements of a dictionary by using the key of the element. Here’s an example of how to access the elements of a dictionary:

# Create a dictionary 
my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}
# Access the value of the 'name' key
name = my_dict['name']
print(name) # Output: 'John'

You can also use the get() method to access the values of a dictionary, which will return None if the key is not found, instead of raising a KeyError.

# Create a dictionary
my_dict = {'name': 'John', 'age': 30, 'city': 'New York'}

# Access the value of the 'gender' key
gender = my_dict.get('gender')
print(gender) # Output: None

Sets

A set is a collection of items that are unordered and unchangeable. Sets are written with curly braces, and elements are separated by commas. Here’s an example of how to create a set:

# Create a set
my_set = {1, 2, 3, 4, 5}
print(my_set) # Output: {1, 2, 3, 4, 5}

You can add or remove elements from a set using the add() and remove() methods, respectively.

List and Dictionary Comprehension

List comprehension and dictionary comprehension are a more concise way to create lists and dictionaries. They are written with a similar syntax to a for loop, but they are more efficient and readable. Here’s an example of how to use list comprehension to create a list of even numbers:

# Create a list of even numbers
even_numbers = [x for x in range(1, 11) if x % 2 == 0]
print(even_numbers) # Output: [2, 4, 6, 8, 10]

And here’s an example of how to use dictionary comprehension to create a dictionary of squares:

# Create a dictionary of squares
squares = {x: x**2 for x in range(1, 11)}
print(squares) # Output: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100}

Conclusion

In this tutorial, we have covered the basics of the different data structures in Python, including lists, tuples, dictionaries, and sets, and how to use them to store and manipulate data. We have also covered some advanced topics such as list comprehension and dictionary comprehension. Remember that each data structure has its own characteristics, and it’s important to choose the right data structure for the task at hand to make the code more efficient and readable.

--

--

Ingo Krause

I'm a chemist with focus on polymers and electrochemistry. Currently working on my PhD-thesis. I'm interested in technology, programming and I love to cook.