
Lists, Tuples, Sets, and Dictionaries in Python
Python has a number of strong built-in data structures that make managing data collections easier. The most often used of them are dictionaries, lists, tuples, and sets.
Let’s examine each one using best practices, salient features, and real-world examples.
๐ 1. Python Lists
โ Definition:
An ordered, mutable (changeable) collection of items is called a list. Items of any data type can be stored in lists.
๐งช Syntax:
my_list = [1, 2, 3, 'apple', True]
๐งฐ Common Operations:
my_list.append('new') # Add element at end
my_list.remove('apple') # Remove element
my_list[0] = 100 # Change value at index
๐ Key Features:
- Ordered (indexed)
- Allows duplicates
- Mutable (modifiable)
You can also read this blog:- Loops in Python: Mastering for and while Loops with Examples
๐ง Use Case:
When you require an organized, adaptable collection, such as a list of chores, cart contents, or student names.
๐ 2. Python Tuples
โ Definition:
An ordered, immutable (unchangeable) collection is called a tuple. Perfect for safeguarding data against alteration.
๐งช Syntax:
my_tuple = (10, 20, 30)
๐ Accessing Elements:
print(my_tuple[1]) # Output: 20
๐ Key Features:
- Ordered
- Immutable
- Faster than lists
- Can contain duplicates
๐ง Use Case:
Best for fixed data like coordinates, database rows, or month names.
๐งบ 3. Python Sets
โ Definition:
A set is an unordered collection of unique elements.
๐งช Syntax:
my_set = {1, 2, 3, 2, 1}
print(my_set) # Output: {1, 2, 3}
๐ Common Operations:
my_set.add(4)
my_set.remove(1)
๐ Key Features:
- Unordered
- No duplicate elements
- Mutable
- Cannot access items by index
๐ง Use Case:
Ideal for carrying out set operations such as intersection, union, and so on, or for eliminating duplicates.
๐ 4. Python Dictionaries
โ Definition:
An unordered collection of key-value pairs is called a dictionary. Every key needs to be distinct.
๐งช Syntax:
my_dict = {'name': 'Alice', 'age': 25}
๐ Common Operations:
print(my_dict['name']) # Access value
my_dict['age'] = 26 # Modify value
my_dict['city'] = 'Mumbai' # Add new key-value pair
๐ Key Features:
- Unordered (Python 3.6+ maintains insertion order)
- Keys must be unique
- Mutable
- Optimized for fast lookups
๐ง Use Case:
Used when you need a mapping of values, such as storing user profiles, API responses, etc.
๐ Comparison Table
Feature | List | Tuple | Set | Dictionary |
---|---|---|---|---|
Ordered | โ | โ | โ | โ (Python 3.7+) |
Mutable | โ | โ | โ | โ |
Duplicate Allowed | โ | โ | โ | Keys: โ, Values: โ |
Indexing | โ | โ | โ | โ (by key) |
Syntax | [ ] | ( ) | { } | {key: value} |
๐งโ๐ป Real-Life Example
students = [
{'name': 'Alice', 'skills': ('Python', 'SQL'), 'marks': {85, 90, 85}},
{'name': 'Bob', 'skills': ('Java', 'HTML'), 'marks': {78, 82, 78}},
]
for student in students:
print(f"{student['name']} knows {student['skills']} and scored {student['marks']}")
โ Conclusion
Writing effective Python code requires an understanding of the distinctions between lists, tuples, sets, and dictionaries. Select the appropriate data structure.
You may be like this:-
Java vs. Kotlin: Which One Should You Learn for Backend Development?
Leave a Reply