Beginner tutorial on Python Data Structures

Bezawit Lake
3 min readMay 28, 2024

--

Python is a powerful, high-level programming language that supports a variety of data structures. In this blog post, we will explore five of the most commonly used data structures in Python: List, Tuple, Set, Dictionary, and Array. We will learn how to create them, access elements, add elements, and remove items.

1. List

A list in Python is an ordered collection of items that can be of any type. It is a collection of items enclosed in square brackets []. They can hold elements of different data types and allow for easy modification, insertion, and deletion of elements.

# Creating a list
my_list = ['Green', 'Yellow', 'Red']
print(my_list) # Output: ['Green', 'Yellow', 'Red']

To access list elements we use index. List indexing starts from 0, where the first element is at index 0, the second element is at index 1, and so on.

# Accessing elements
print(my_list[0]) # Output: 'Red'
# Adding elements
my_list.append('White')
print(my_list) # Output: ['Green', 'Yellow', 'Red','White']
# Removing items
my_list.remove('Yellow')
print(my_list) # Output: ['Green', 'Red','White']
Python

2. Array

Array are used to store homogeneous data elements of a fixed size. The array module in Python provides an array data structure that is more efficient than lists for certain operations involving numerical data.

import array

my_array = array.array('i', [1, 2, 3, 4, 5]) #output: array('i', [1, 2, 3, 4, 5])

# Accessing elements
print(my_array[0]) # Output: 1
# Adding elements
my_array.append(6)
print(my_array) # Output: array('i', [1, 2, 3, 4, 5, 6])
# Removing items
my_array.remove(2)
print(my_array) # Output: array('i', [1, 3, 4, 5, 6])

3. Sets

Sets are unordered collections of unique elements. They are enclosed in curly braces {} or can be created using the set() function. Sets are useful for eliminating duplicates and performing mathematical set operations like union, intersection, and difference.

# Creating a set
my_set = {'Green', 'Yellow', 'Red'}
print(my_set) # Output: {'Green', 'Red', 'Yellow'}

In Python, sets are unordered collections of unique elements. Since sets are not indexed, we cannot directly access individual elements using indexing like we would with lists or arrays. However, we can iterate over the elements of a set or use membership testing to check if a specific element is present.

# accessing elements in a set
for element in my_set:
print(element)

# Adding elements
my_set.add('White')
print(my_set) # Output: {'Green', 'Red', 'White', 'Yellow'}
# Removing items
my_set.remove('Red')
print(my_set) # Output: {'Green', 'White', 'Yellow'}

3. Tuple

Tuples are similar to lists but are immutable, meaning their elements cannot be modified once defined. They are enclosed in parentheses () and are often used to store related pieces of data together.

my_tuple = (10, 20, 30, 'Green', 'Yellow', 'Red')
print(my_tuple) # Output: (10, 20, 30, 'Green', 'Yellow', 'Red')

tuples are ordered collections of elements, and each element in a tuple can be accessed using indexing.

# Accessing elements
print(my_tuple[0]) # Output: '10'

# Adding or removing elements in tuple is not possible
# because it's immutable

4. Dictionary

Dictionaries are key-value pairs enclosed in curly braces {}. Each element in a dictionary consists of a unique key and its corresponding value. Dictionaries are efficient for accessing values based on their keys.

# Creating a dictionary
my_dict = {'color_1': 'Green', 'color_2': 'Yellow','color_3': 'Red'}
print(my_dict) # Output: {'color_1': 'Green', 'color_2': 'Yellow', 'color_3': 'Red'}

Dictionaries are key-value pairs, and you can access individual elements of a dictionary using keys. Dictionaries are unordered collections, so they do not support indexing.

# 1. Accessing elements
print(my_dict.keys()) # Output: ['color_1', 'color_2', 'color_3']
print(my_dict.values()) #Output: (['Green', 'Yellow', 'Red'])

# 2. Accesing elements
print(my_dict['color_1']) # Output: 'Green'
# Adding elements
my_dict['color_4'] = 'White'
print(my_dict) # Output: {'color_1': 'Green', 'color_2': 'Yellow', 'color_3': 'Red', 'color_4': 'White'}
# Removing items
del my_dict['color_4']
print(my_dict) # Output: {'color_1': 'Green', 'color_2': 'Yellow', 'color_3': 'Red'}

In conclusion, Python provides a variety of data structures that we can use to store, organize, and manipulate our data. By understanding these data structures and how to use them, we can write more efficient and effective Python code.

--

--