DEV Community

Mohammad Nadeem
Mohammad Nadeem

Posted on

Mastering Python Collections: List, Tuple, Set, and Dictionary

1. Python Lists – The Ordered All-Rounder
A list is a mutable, ordered collection that can store items of different types.

Key Features:

  • Ordered
  • Mutable (can change)
  • Allows duplicates

💡 Syntax & Example:

fruits = ['apple', 'banana', 'cherry']
fruits.append('mango')
print(fruits)  # ['apple', 'banana', 'cherry', 'mango']

Enter fullscreen mode Exit fullscreen mode

🔧 Common List Operations:

fruits[0]         # Access first item → 'apple'
fruits[-1]        # Last item → 'mango'
len(fruits)       # 4
'banana' in fruits  # True
fruits.remove('apple')
Enter fullscreen mode Exit fullscreen mode

🔒 2. Python Tuples – The Immutable Sibling

A tuple is like a list, but immutable (cannot be changed after creation).

Key Features:

  • Ordered
  • Immutable
  • Allows duplicates

💡 Syntax & Example:

person = ('Alice', 30, 'Engineer')
print(person[1])  # 30
Enter fullscreen mode Exit fullscreen mode

⚠️ Single-Element Tuples:

t = ('hello',)  # With comma — valid tuple
not_a_tuple = ('hello')  # Just a string!
Enter fullscreen mode Exit fullscreen mode

When to use:

  • Fixed data structures (e.g., coordinates, DB rows)
  • As keys in dictionaries (if they contain only immutable types)

🧹 3. Python Sets – The Unique Collection
A set is an unordered, mutable collection of unique items.

Key Features:

  • Unordered
  • No duplicates
  • Fast membership tests

💡 Syntax & Example:

colors = {'red', 'green', 'blue'}
colors.add('yellow')
colors.add('red')  # Ignored, already exists
print(colors)  # {'green', 'blue', 'red', 'yellow'}
Enter fullscreen mode Exit fullscreen mode

🔧 Common Set Operations:

colors.remove('blue')
'aqua' in colors         # False
colors.union({'black'})  # New set with combined items
Enter fullscreen mode Exit fullscreen mode

⚠️ Empty set:

s = set()     # ✅ This is an empty set
s2 = {}       # ⚠️ This is an empty dictionary!
Enter fullscreen mode Exit fullscreen mode

🧠 4. Python Dictionaries – The Key-Value Store

A dictionary (or dict) stores data as key-value pairs.

Key Features:

  • Unordered (in < Python 3.7)
  • Keys must be unique and immutable
  • Values can be of any type

💡 Syntax & Example:

user = {
    'name': 'John',
    'age': 25,
    'email': 'john@example.com'
}
print(user['name'])  # John
Enter fullscreen mode Exit fullscreen mode

🔧 Common Dictionary Operations:

user['age'] = 26         # Update
user.get('city', 'N/A')  # Safe lookup → 'N/A'
del user['email']        # Remove key
'user' in user           # False
Enter fullscreen mode Exit fullscreen mode

Nested Dict Example:

employee = {
    'id': 101,
    'profile': {'name': 'Jane', 'role': 'Manager'}
}
print(employee['profile']['name'])  # Jane
Enter fullscreen mode Exit fullscreen mode

Final Thoughts
Understanding these core Python data structures is essential for effective programming. Choose the right one based on your needs:

  • Use lists for general-purpose ordered data
  • Use tuples for fixed-size, immutable groups
  • Use sets for fast membership checks and unique values
  • Use dictionaries when you need to map keys to values

🗨️ What’s Next?
Want to go deeper? In future posts, I’ll cover:

  • Custom sorting of lists/dicts
  • Advanced set operations
  • Dictionary comprehensions
  • Real-world use cases in APIs and data processing

Follow me for more Python tips and tricks! 🐍✨

Top comments (0)