Python data structures

Key takeaways:

  • Python’s built-in data structures i.e. lists, tuples, dictionaries and sets are simple to use and come with methods for common tasks like adding, removing, or searching items.

  • Lists are ordered and flexible, making them perfect for tasks like managing a dynamic to-do list or storing items in sequence.

  • Tuples are ordered but immutable, meaning they’re ideal for storing data that shouldn’t change, like dates, coordinates, or configuration settings.

  • Sets store only unique items and are unordered. They’re great for removing duplicates or performing operations like unions and intersections.

  • Dictionaries map keys to values, making them efficient for organizing and retrieving data like a phonebook or a product catalog.

  • Each Python data structure serves a purpose and is suitable for different needs like you'll use lists for flexibility, tuples for fixed data, sets for uniqueness, and dictionaries for fast lookups.

In Python, we have four built-in data structures:

Let’s discuss each one of these one by one:

List

A data structure that stores an ordered collection of items in Python is called a list.

Defining a list

A list is generally represented within square brackets [] and can contain elements of different data types.

# A list is being defined and printed
l1 = [1,2,3,4,5,6]
print(l1)
# print(l1[ : ])
List and its operations in Python

Methods of lists

Below is a table of commonly used list methods, along with their descriptions:

Method

Description

append()

Adds an element to the end of the list.

copy()

Returns a shallow copy of the list.

pop()

Eliminates and returns an element from the list.

sort()

Sorts all elements of a list in ascending order.

reverse()

Reverses the order of all elements in the list.

remove()

Eliminates an element from the list.

Here is a code example to demonstrate the use of these Python functions:

# Initialize a list
numbers = [1,2,3,4,5,6]
# 1. append() function
numbers.append(10)
print("After append(10):", numbers)
# 2. copy() function
numbers_copy = numbers.copy()
print("Copied list:", numbers_copy)
# 3. pop() function
popped_element = numbers.pop()
print("After pop():", numbers)
print("Popped element:", popped_element)
# 4. sort() function
numbers.sort()
print("After sort():", numbers)
# 5. reverse() function
numbers.reverse()
print("After reverse():", numbers)
# 6. remove() function
numbers.remove(5)
print("After remove(5):", numbers)
A code example demonstrating methods of Python lists

Tuple

Similar to a list, the tuple is a built-in data structure in Python. Tuples are used in scenarios where it is certain that the(set of) values belonging to some statement or a user-defined function will not change.

Defining a tuple

A tuple is defined with parentheses (), and each item is separated by commas ,. Generally, a tuple is defined as follows:

t=(1,2,3,"Hello",3.14)
print(t)
Tuple and its operations in Python

The most important difference between a list and a tuple is mutability. Unlike lists, tuples are immutable, i.e., they can’t be modified.

Methods of tuples

Since tuples are immutable, they do not support methods like append(), remove(), or sort() found in lists. However, they do support a few built-in methods that primarily focus on querying their contents. Below is the table that summarizes the tuple methods:

Method

Description

count(value)

Returns the number of occurrences of the specified value in the tuple.

index(value)

Returns the index of the first occurrence of the specified value. Raises a ValueError if the value is not found.

Here is a code example to demonstrate the use of these Python functions:

# Define a tuple
t = (1, 2, 3, 2, 4, 2, 5)
# 1. count(value): Count the occurrences of a value in the tuple
count_of_2 = t.count(2)
print("Count of 2 in t:", count_of_2)
# 2. index(value): Find the index of the first occurrence of a value
index_of_3 = t.index(3)
print("Index of 3 in t:", index_of_3)
# Attempting index() with a non-existent value will raise an error
try:
t.index(10)
except ValueError as e:
print("Error:", e)
A code example demonstrating methods of Python tuples

Dictionary

A dictionary is an unordered, changeable, and indexed collection that stores data in the form of key-value pairs. The key defined for a dictionary needs to be unique.

Defining a dictionary

A dictionary is generally represented in the form of curly brackets {} with the key and value separated by a colon :. The key-value pairs in a dictionary don’t follow any specific order.

# Define the dictionary
d = {"car":"Baleno",
"model":"GT500",
"year":2020
}
print(d)
Dictionary and its operations in Python

Methods of dictionaries

Dictionaries have the following built-in methods:

Method

Description

items()

Returns a new object of the dictionary’s items in (key,value) format.

keys()

Returns a new object of the dictionary’s keys.

values()

Returns a new object of the dictionary’s values.

copy()

Returns a shallow copy of the dictionary.

clear()

Removes all elements from the dictionary.

get(<key>)

Retrieves the value for a specific key.

popitem()

Removes and returns the last inserted item (key,value).

Here is a code example to demonstrate the use of these Python functions:

# Define the dictionary
d = {"car":"Baleno",
"model":"GT500",
"year":2020
}
# Accessing all key-value pairs
items = d.items()
print("Items (key-value pairs):", items)
# Accessing all keys
keys = d.keys()
print("Keys:", keys)
# Accessing all values
values = d.values()
print("Values:", values)
# Copying the dictionary
copydict = d.copy()
print(copydict)
# Clearing all entries from the dictionary
d.clear()
print("Dictionary after clearing:", d)
# Resetting the dictionary for next functions
d = copydict.copy()
# Retrieving a value for a specific key
car_model = d.get("model")
print("Model of the car:", car_model)
# Removing a key-value pair using popitem()
removed_value = d.popitem()
print("Removed item:", removed_value)
print("Dictionary after removal:", d)
# Checking if a key exists
has_key = "car" in d
print("Does the key 'car' exist?", has_key)
A code example demonstrating methods of Python dictionary

Sets

A set is an unordered and unindexed collection. In Python, sets are written with curly braces. In addition to being iterable and mutable, a set has no duplicate elements.

Defining a set

In Python, a set is defined using curly braces {} or the set() constructor. In this answer, we’ll use the curly braces for defining the set.

# Define a set
my_set = {1, 2, 3, 4}
# Print the set
print("Defined set:", my_set)

Methods of sets

Method

Description

add()

Adds an element to the set.

remove()

Removes a specified element; raises an error if the element does not exist.

discard()

Removes a specified element without raising an error if it doesn’t exist.

clear()

Removes all elements from the set.

pop()

Removes and returns an arbitrary element from the set.

union()

Returns a new set containing all elements from both sets.

intersection()

Returns a new set containing only the common elements between sets.

difference()

Returns a new set with elements in the first set but not in the second.

symmetric_difference()

Returns a new set with elements in either set but not both.

update()

Adds elements from another set or iterable to the set.

issubset()

Checks if the set is a subset of another set.

issuperset()

Checks if the set is a superset of another set.

isdisjoint()

Checks if two sets have no elements in common.

Here is a code example to demonstrate the use of these Python functions:

# Define two sets
set_a = {1, 2, 3, 4}
set_b = {3, 4, 5, 6}
# dd an element to the set
set_a.add(5)
print("After add(5):", set_a)
# Remove an element (raises an error if the element is not found)
set_a.remove(5)
print("After remove(5):", set_a)
# Remove an element (doesn't raise an error if not found)
set_a.discard(6) # No error even if 6 is not in set_a
print("After discard(6):", set_a)
# Remove all elements from the set
temp_set = set_a.copy() # Make a copy to preserve the original
temp_set.clear()
print("After clear():", temp_set)
# Remove and return an arbitrary element
popped_element = set_a.pop()
print("After pop():", set_a, "| Popped element:", popped_element)
# Combine elements from both sets (returns a new set)
union_set = set_a.union(set_b)
print("Union of set_a and set_b:", union_set)
# 7. Find common elements between sets (returns a new set)
intersection_set = set_a.intersection(set_b)
print("Intersection of set_a and set_b:", intersection_set)
# Find elements in set_a but not in set_b
difference_set = set_a.difference(set_b)
print("Difference (set_a - set_b):", difference_set)
# Find elements in either set_a or set_b but not both
symmetric_difference_set = set_a.symmetric_difference(set_b)
print("Symmetric difference of set_a and set_b:", symmetric_difference_set)
# Add elements from set_b to set_a
set_a.update(set_b)
print("After update(set_b):", set_a)
# Check if set_a is a subset of set_b
print("Is set_a a subset of union_set?", set_a.issubset(union_set))
# Check if set_a is a superset of set_b
print("Is set_a a superset of set_b?", set_a.issuperset(set_b))
# Check if set_a and set_b have no elements in common
print("Are set_a and set_b disjoint?", set_a.isdisjoint(set_b))
A code example demonstrating methods of Python set

Choosing the Right Data Structure in Python

Now that we've explored the builtin data structures of Python, we must take into account that while working with data, selecting the right data structure is important for better performance and code clarity. Below is a table to guide you on which Python data structure to use based on your needs:

Data Structure

When to Use

Example Scenario

List

Use when you need an ordered, mutable collection of items that may contain duplicates. Good for dynamic arrays where you need to add or remove items.

Managing a to-do list where tasks can be added, removed, or rearranged dynamically.

Tuple

Use when you need an ordered, immutable collection of items. Ideal for fixed data that shouldn’t change, such as coordinates or configuration values.

Storing the details of a date of birth (year, month, day) or an immutable combination like product ID and version number

Dictionary

Use when you need to store key-value pairs for fast lookups. Perfect for scenarios where each item has a unique identifier (e.g., usernames or IDs).

Creating a phonebook where names (keys) are linked to their phone numbers (values), enabling quick lookups by name.

Set

Use when you need a collection of unique, unordered items. Great for removing duplicates or performing set operations like union or intersection.

Keeping track of unique email addresses collected from a signup form to avoid sending duplicate invitations.

Excited to learn more on Python programing? Explore these beginner level Educative projects to get hands on with Python programing:

Frequently asked questions

Haven’t found what you were looking for? Contact Us


What are the Python data structures?

Python has two types of data structures: 1.Built-in data structures that include List (An ordered, mutable collection of items), Tuple (An ordered, immutable collection of items), Set (An unordered collection of unique items) and Dictionary (A collection of key-value pairs for fast lookups).

2.Data structures from libraries: Python libraries like NumPy and pandas offer advanced data structures such as arrays and DataFrames, which are optimized for numerical and tabular data manipulation.


What is a built-in data structure in Python?

A built-in data structure in Python is a pre-defined way to organize and manage data, provided directly by Python. Examples include lists, tuples, sets, and dictionaries.


Free Resources