Site icon Full-Stack

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:

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:

🧠 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:

🧠 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:

🧠 Use Case:

Used when you need a mapping of values, such as storing user profiles, API responses, etc.

πŸ”„ Comparison Table

FeatureListTupleSetDictionary
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?

Where to Find Your Salesforce Organization ID

How Salesforce Stands Out from Other CRMs

Exit mobile version