Blog 5: Mastering Lists, Tuples, Dictionaries, and Sets in Python

Alisha MalhotraAlisha Malhotra
4 min read

Introduction

In Python, data structures like lists, tuples, dictionaries, and sets are fundamental for storing and managing data efficiently. Each has unique characteristics suited for different use cases. In this blog, we will explore each structure in detail with syntax, features, and practical examples to help you understand when and how to use them.


1. Lists in Python

Definition:

A list is an ordered, mutable collection that can contain elements of different data types.

my_list = [1, "hello", 3.5, True]

Features:

  • Lists are ordered

  • Mutable (can be modified)

  • Allow duplicate values

  • Can store different data types

Creating Lists:

  • Using square brackets:
fruits = ["apple", "banana", "cherry"]
  • Using list constructor:
numbers = list((1, 2, 3))
  • Using list comprehension:
even_numbers = [x for x in range(10) if x % 2 == 0]

Accessing Elements:

  • Positive indexing:
print(fruits[0])  # apple
  • Negative indexing:
print(fruits[-1])  # cherry
  • Slicing:
print(fruits[0:2])  # ['apple', 'banana']
  • Looping:
for item in fruits:
    print(item)

Operators with Lists:

  • Repetition:
print(fruits * 2)
  • Concatenation:
print(fruits + ["orange"])
  • Membership:
print("apple" in fruits)  # True

List Methods:

fruits.append("orange")
fruits.remove("banana")
fruits.insert(1, "kiwi")
fruits.sort()
fruits.reverse()

Built-in Functions:

len(fruits)
sum([1, 2, 3])
min([5, 2, 9])
max([1, 8, 3])

Copying Lists:

copy_list = fruits[:]

Mutability:

fruits[0] = "mango"  # Lists are mutable

Nested Lists:

matrix = [[1, 2], [3, 4], [5, 6]]

2. Tuples in Python

Definition:

A tuple is an ordered, immutable collection of elements.

Features:

  • Ordered

  • Immutable

  • Allows duplicate values

Creating Tuples:

  • Using parentheses:
tuple1 = (1, 2, 3)
  • Using tuple constructor:
tuple2 = tuple(["a", "b"])

Accessing Elements:

  • Positive & negative indexing:
print(tuple1[0])
print(tuple1[-1])
  • Looping:
for item in tuple1:
    print(item)

Operators:

print(tuple1 * 2)
print(tuple1 + (4, 5))
print(2 in tuple1)

Tuple Methods:

tuple1.count(2)
tuple1.index(3)

Built-in Functions:

len(tuple1)
sum((1, 2, 3))

Tuple Assignment:

a, b = (10, 20)  # Unpacking

3. Dictionaries in Python

Definition:

A dictionary is an unordered, mutable collection of key-value pairs.

Features:

  • Keys must be unique and immutable

  • Values can be duplicated

Creating Dictionaries:

  • Using curly brackets:
student = {"name": "Alice", "age": 21}
  • Using constructor:
d = dict(name="Bob", age=22)
  • Using comprehension:
squares = {x: x**2 for x in range(5)}

Retrieving/Adding/Modifying:

print(student["name"])
student["grade"] = "A"
student["age"] = 22

Nested Dictionary:

students = {
  "student1": {"name": "Alice", "age": 21},
  "student2": {"name": "Bob", "age": 22}
}

4. Sets in Python

Definition:

A set is an unordered, mutable, and unindexed collection of unique elements.

Features:

  • No duplicates

  • Unordered

  • Mutable

Creating Sets:

  • Using curly braces:
set1 = {1, 2, 3}
  • Using constructor:
set2 = set([4, 5, 6])
  • Using comprehension:
set3 = {x for x in range(10) if x % 2 == 0}

Adding/Removing Elements:

set1.add(4)
set1.remove(2)

Set Comparison:

A = {1, 2, 3}
B = {1, 2}
print(B.issubset(A))
print(A.issuperset(B))

Set Methods:

set1.union(set2)
set1.intersection(set2)
set1.difference(set2)
set1.symmetric_difference(set2)

Personal Experience

When I was learning Python’s built-in data structures, I kept running into confusion—especially with similar-looking syntax like {} (which could mean either a set or dictionary) and with how lists and tuples felt nearly identical at first glance. I remember writing a few practice problems where I tried modifying tuple elements and kept getting errors—it finally hit me that mutability is the key difference between them.

What really helped was creating comparison tables and trying out small code snippets side by side. I’d make one version using a list, another with a tuple, and then try the same logic with a set or dictionary to see what failed or behaved differently. That hands-on contrast helped me remember that:

  • Tuples are like frozen lists

  • Sets hate duplicates

  • Dictionaries thrive on keys, and

  • Lists are the most flexible playground when you're just starting out.

Now I no longer mix them up and can confidently choose the right one based on the task!

Sneak Peek: What’s Next?

If you’ve ever wondered how Python handles text or what the difference is between "a", 'a', and '''a'''—stay tuned! My next blog will uncover the world of strings in Python—from basic operations to advanced formatting, slicing, and real-world tricks. You don’t want to miss it!

0
Subscribe to my newsletter

Read articles from Alisha Malhotra directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Alisha Malhotra
Alisha Malhotra