Collections in Python
Lists (list)
Lists in Python are ordered, mutable collections. They can hold items of different types and support operations such as adding, removing, and modifying elements.
Characteristics:
- Ordered: Elements have a defined order.
- Mutable: You can change the list after creation.
- Allows duplicates: Lists can contain repeated elements.
Syntax:
my_list = [1, 2, 3, "hello", [4, 5]]
Operations Examples:
# Creating a list fruits = ["apple", "banana", "cherry"] # Adding an element fruits.append("orange") # Inserting an element at a specific position fruits.insert(1, "blueberry") # Removing an element fruits.remove("banana") # Accessing an element print(fruits[0]) # Outputs: 'apple' # Getting the length of the list print(len(fruits)) # Outputs: 4
Tuples (tuple)
Tuples are ordered, immutable collections. Once created, tuples cannot be changed.
Characteristics:
- Ordered: Elements have a defined order.
- Immutable: You cannot modify elements after creation.
- Allows duplicates: Tuples can contain repeated elements.
Syntax:
my_tuple = (1, 2, 3, "hello", (4, 5))
Operations Examples:
# Creating a tuple point = (10, 20) # Accessing an element print(point[1]) # Outputs: 20 # Unpacking a tuple x, y = point print(x, y) # Outputs: 10 20 # Getting the length of the tuple print(len(point)) # Outputs: 2
Sets (set)
Sets are unordered, mutable collections designed to store unique elements.
Characteristics:
- Unordered: Elements have no specific order.
- Mutable: You can add or remove elements.
- No duplicates: Sets do not allow repeated elements.
Syntax:
my_set = {1, 2, 3, "hello"}
Operations Examples:
# Creating a set colors = {"red", "green", "blue"} # Adding an element colors.add("yellow") # Removing an element colors.discard("green") # Set operations set1 = {1, 2, 3} set2 = {3, 4, 5} # Union print(set1 | set2) # Outputs: {1, 2, 3, 4, 5} # Intersection print(set1 & set2) # Outputs: {3} # Difference print(set1 - set2) # Outputs: {1, 2}
Dictionaries (dict)
Dictionaries are ordered collections (from Python 3.7 onwards) that store key-value pairs.
Characteristics:
- Ordered: Elements are stored in the order of insertion (Python 3.7+).
- Mutable: You can add, remove, or modify elements.
- Unique keys: Keys must be unique within a dictionary.
Syntax:
my_dict = {"name": "Alice", "age": 30, "city": "Paris"}
Operations Examples:
# Creating a dictionary person = {"name": "Alice", "age": 30} # Adding an element person["city"] = "Paris" # Modifying an element person["age"] = 31 # Removing an element del person["city"] # Accessing a value print(person["name"]) # Outputs: 'Alice' # Getting keys and values print(person.keys()) # Outputs: dict_keys(['name', 'age']) print(person.values()) # Outputs: dict_values(['Alice', 31])
Collections of Collections
Python also allows the creation of collections of collections, such as lists of dictionaries or sets of tuples.
Examples:
# List of dictionaries employees = [ {"name": "Alice", "position": "Engineer"}, {"name": "Bob", "position": "Manager"} ] # Set of tuples coordinates = {(10, 20), (30, 40), (50, 60)}
Summary
- Lists (list): Ordered and mutable collections, allow duplicates.
- Tuples (tuple): Ordered and immutable collections, allow duplicates.
- Sets (set): Unordered and mutable collections, no duplicates.
Dictionaries (dict): Ordered (Python 3.7+), mutable collections of key-value pairs, unique keys.