Comparison of Operations for Python Data Structures
Python collections, such as lists, dictionaries, sets, and tuples, share some common operations. These operations are crucial for manipulating and accessing data within these collections efficiently. The table below summarises the most common operations and their applicability across different collection types.
This page provides a comparison of key operations available for the fundamental data structures in Python: Lists, Dictionaries, Sets, and Tuples. For each operation, we provide a brief explanation and a code snippet demonstrating its usage.
Access by Index/Key
Access by Index/Key
Data Structure |
Code Example |
List |
my_list = [10, 20, 30]
element = my_list[1]
print(element) # Output: 20
|
Dictionary |
my_dict = {"a": 1, "b": 2}
value = my_dict["a"]
print(value) # Output: 1
|
Set |
my_set = {1, 2, 3}
# Sets do not support index-based access
print(1 in my_set) # Output: True
|
Tuple |
my_tuple = (10, 20, 30)
element = my_tuple[1]
print(element) # Output: 20
|
Adding Elements
Adding Elements
Data Structure |
Code Example |
List |
my_list = [10, 20, 30]
my_list.append(40)
print(my_list) # Output: [10, 20, 30, 40]
|
Dictionary |
my_dict = {"a": 1, "b": 2}
my_dict["c"] = 3
print(my_dict) # Output: {"a": 1, "b": 2, "c": 3}
|
Set |
my_set = {1, 2, 3}
my_set.add(4)
print(my_set) # Output: {1, 2, 3, 4}
|
Tuple |
# Tuples are immutable, elements cannot be added
|
Removing Elements
Removing Elements
Data Structure |
Code Example |
List |
my_list = [10, 20, 30]
my_list.remove(20)
print(my_list) # Output: [10, 30]
|
Dictionary |
my_dict = {"a": 1, "b": 2}
del my_dict["b"]
print(my_dict) # Output: {"a": 1}
|
Set |
my_set = {1, 2, 3}
my_set.discard(2)
print(my_set) # Output: {1, 3}
|
Tuple |
# Tuples are immutable, elements cannot be removed
|
Iteration
Iteration
Data Structure |
Code Example |
List |
my_list = [10, 20, 30]
for element in my_list:
print(element)
|
Dictionary |
my_dict = {"a": 1, "b": 2}
for key in my_dict:
print(key, my_dict[key])
|
Set |
my_set = {1, 2, 3}
for element in my_set:
print(element)
|
Tuple |
my_tuple = (10, 20, 30)
for element in my_tuple:
print(element)
|
Membership Check
Membership Check
Data Structure |
Code Example |
List |
my_list = [10, 20, 30]
print(20 in my_list) # Output: True
|
Dictionary |
my_dict = {"a": 1, "b": 2}
print("a" in my_dict) # Output: True
|
Set |
my_set = {1, 2, 3}
print(2 in my_set) # Output: True
|
Tuple |
my_tuple = (10, 20, 30)
print(20 in my_tuple) # Output: True
|
Concatenation
Concatenation
Data Structure |
Code Example |
List |
list1 = [10, 20]
list2 = [30, 40]
result = list1 + list2
print(result) # Output: [10, 20, 30, 40]
|
Dictionary |
# Dictionaries do not support direct concatenation
|
Set |
# Sets do not support direct concatenation
|
Tuple |
tuple1 = (10, 20)
tuple2 = (30, 40)
result = tuple1 + tuple2
print(result) # Output: (10, 20, 30, 40)
|
Slicing
Slicing
Data Structure |
Code Example |
List |
my_list = [10, 20, 30, 40]
slice = my_list[1:3]
print(slice) # Output: [20, 30]
|
Dictionary |
# Dictionaries do not support slicing
|
Set |
# Sets do not support slicing
|
Tuple |
my_tuple = (10, 20, 30, 40)
slice = my_tuple[1:3]
print(slice) # Output: (20, 30)
|
Key Takeaway
Understanding the common operations across Python collections is essential for effective data management. Each collection type—lists, dictionaries, sets, and tuples—supports specific operations that make them suitable for different tasks. By mastering these operations, you can manipulate data more efficiently in your Python programs.
Each Python data structure—List, Dictionary, Set, and Tuple—offers different operations that make them suitable for various use cases. Understanding these operations and how they differ across data structures will help you choose the right one for your specific needs.