Lists, Tuples, Sets, and Dictionaries in Python

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

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

admin
admin
https://www.thefullstack.co.in

Leave a Reply