Lists
Lists are ordered, mutable collections that can hold a variety of
object types. They are one of the most versatile data structures in Python.
# Creating a list
my_list = [1, 2, 3, 'apple', 'banana']
# Accessing elements
first_item = my_list[0] # 1
(zero-based indexing)
# Modifying elements
my_list[1] = 'orange'
# Common methods
my_list.append('grape') # Add to
end
my_list.insert(2, 'pear') # Insert at
position
removed_item = my_list.pop() # Remove and return last item
Lists support slicing, concatenation, and many other operations that make
them extremely useful for storing collections of items.
Tuples
Tuples are similar to lists but are immutable (cannot be changed after
creation). They are often used for fixed collections of items.
# Creating a tuple
my_tuple = (1, 'apple',
3.14)
# Accessing elements
second_item = my_tuple[1] #
'apple'
# Tuple unpacking
a, b, c = my_tuple # a=1, b='apple', c=3.14
# Single element tuple (note the comma)
single_tuple = (42,)
Tuples are more memory efficient than lists and can be used as dictionary
keys (since they're immutable).
Dictionaries
Dictionaries store key-value pairs and provide fast lookups. Keys must
be immutable types (strings, numbers, tuples).
# Creating a dictionary
student = {
'name': 'Alice',
'age': 22,
'courses': ['Math', 'Physics']
}
# Accessing values
name = student['name'] #
'Alice'
age = student.get('age') #
22
# Adding/updating items
student['grade'] = 'A'
student.update({'age': 23, 'city': 'New York'})
# Dictionary methods
keys = student.keys() # ['name', 'age', 'courses', 'grade',
'city']
values = student.values() # ['Alice', 23, ['Math', 'Physics'], 'A',
'New York']
Sets
Sets are unordered collections of unique elements. They are useful for
membership testing and eliminating duplicate entries.
# Creating a set
fruits = {'apple', 'banana', 'orange'}
numbers = set([1, 2,
3, 2, 1]) # {1, 2, 3}
# Set operations
fruits.add('pear')
fruits.remove('banana')
# Set methods
a = {1, 2, 3}
b = {3, 4, 5}
union = a.union(b) # {1, 2, 3, 4, 5}
intersection = a.intersection(b) # {3}
difference = a.difference(b) # {1, 2}
Advanced Data Structures
Python also offers more specialized data structures in the collections
module.
# Importing from collections module
from collections import defaultdict, Counter, deque, namedtuple
# defaultdict - dictionary with default values
word_counts = defaultdict(int)
word_counts['apple'] += 1 # No KeyError
# Counter - counts hashable objects
colors = ['red', 'blue',
'red', 'green', 'blue', 'red']
color_counts = Counter(colors) # {'red': 3, 'blue': 2, 'green':
1}
# deque - double-ended queue
d = deque([1, 2, 3])
d.appendleft(0) # [0, 1, 2,
3]
d.pop() # removes 3
# namedtuple - tuple with named fields
Point = namedtuple('Point', ['x', 'y'])
p = Point(10, 20)
p.x # 10
p.y # 20
Python Data Structures Videos
Master Python data structures with these handpicked YouTube
tutorials:
Master sequence types in Python:
Working with hash-based structures:
More complex data organizations:
Practical applications and challenges: