Data Structures in Python: A Complete Guide with Examples

   



Python offers several built-in data structures that allow you to manage, organize, and manipulate data efficiently. Understanding data structures is essential for writing optimized, performant code.

🔗 Do you like Techelopment? Check out the site for all the details!

List of data structures

The data structures available in Python are mainly divided into:
  • List: ordered and mutable collections (can be modified after their creation and maintain their insertion order).

  • Tuple: similar to lists, but immutable (cannot be modified after creation).

  • Dictionary: unordered collections of key-value pairs (as of Python 3.7, they preserve insertion order, but do not allow duplicate keys).

  • Set: collections of unique, unordered items (do not retain insertion order and do not allow duplicates).

  • Queue: FIFO (First In, First Out) data structures, where the first element inserted is the first to be removed.

  • Stack: LIFO (Last In, First Out) data structures, where the last element inserted is the first to be removed.

Below, we will analyze each of these structures in detail with practical examples.

1. List

Lists in Python are ordered, mutable collections that can contain elements of different types. "Ordered" means that the order of the elements remains unchanged, while "mutable" means that elements can be added, removed, or modified.

Example

# Creating a list
fruits = ["apple", "banana", "orange"]

# Adding an element
fruits.append("kiwi")

# Removing an element
fruits.remove("banana")

# Accessing an element
print(fruits[0])  # Output: apple

2. Tuple

Tuples are similar to lists, but they are immutable. "Immutable" means that once a tuple is created, it cannot be modified (you cannot add, remove, or change elements).

Example

# Creating a tuple
coordinates = (10, 20)

# Accessing elements
x, y = coordinates
print(x)  # Output: 10
print(y)  # Output: 20

# Direct access to elements
print(coordinates[0])  # Output: 10
print(coordinates[1])  # Output: 20

3. Dictionary

Dictionaries are unordered, mutable collections of key-value pairs. However, starting with Python 3.7, the insertion order is preserved. Each key is unique, allowing quick access to its corresponding values.

Example

# Creating a dictionary
person = {"name": "Mario", "age": 30, "city": "Rome"}

# Accessing values
print(person["name"])  # Output: Mario

# Adding a new element
person["profession"] = "engineer"

# Removing an element
del person["city"]

# Updating an element
person["age"] = 31

4. Set

Sets are unordered, mutable collections of unique elements. "Unordered" means that the elements have no fixed order, while "mutable" means that elements can be added or removed. Also, "unique" means that there can be no duplicates.

Example

# Creating a set
numbers = {1, 2, 3, 4}

# Adding an element
numbers.add(5)

# Removing an element
numbers.remove(3)

# Checking if an element exists
print(2 in numbers)  # Output: True

# Union of two sets
other_numbers = {3, 4, 5, 6}
union = numbers | other_numbers
print(union)  # Output: {1, 2, 3, 4, 5, 6}

# Intersection of two sets
intersection = numbers & other_numbers
print(intersection)  # Output: {4, 5}

5. Queue

Queues are FIFO (First In, First Out) data structures, meaning the first item added is the first item removed. Python provides the queue module to manage them efficiently.

Example

from queue import Queue

# Creating a queue
queue = Queue()

# Adding elements
queue.put("a")
queue.put("b")
queue.put("c")

# Removing an element
print(queue.get())  # Output: a

6. Stack

Stacks follow the LIFO (Last In, First Out) logic, where the last element inserted is the first to be removed. They can be implemented with lists or with queue.LifoQueue.

Example

from queue import LifoQueue

# Creating a stack
stack = LifoQueue()

# Adding elements
stack.put("x")
stack.put("y")
stack.put("z")

# Removing the last added element
print(stack.get())  # Output: z


Summary table


(*) Starting with Python 3.7, dictionaries preserve insertion order, but are technically not considered "ordered" like lists or tuples.

Queues and Stacks follow a logical order for inserting and removing elements (FIFO for queues, LIFO for stacks), but they do not allow arbitrary index-based access to elements like lists or tuples. For this reason, they are not technically considered ordered data structures in the traditional sense, even though they do have a predefined processing order.


Follow me #techelopment

Official site: www.techelopment.it
facebook: Techelopment
instagram: @techelopment
X: techelopment
Bluesky: @techelopment
telegram: @techelopment_channel
whatsapp: Techelopment
youtube: @techelopment