Home Coding & Scripting Data Structures in Python (Lists, Dictionaries, Tuples, Sets)

Data Structures in Python (Lists, Dictionaries, Tuples, Sets)

304
0

commonly used built-in data structures are:

  • Lists
  • Dictionaries
  • Tuples
  • Sets

Let’s look at each one with examples.


📝 If You Want to Practice the Code

If you plan to practice the Python code mentioned within this blog post, you’ll need a working Python environment. You can execute the commands in one of the following ways (choose only one option):

👉 Choose whichever environment you’re most comfortable with. If you haven’t set up Python yet, start with one of the installation posts before trying the examples here.


🔹 Lists

A list is an ordered collection of items. You can store numbers, strings, or even other lists. Lists are mutable, meaning you can change them after creation.

fruits = ["apple", "banana", "cherry"]
print(fruits[0])   # apple

fruits.append("mango")   # add item
print(fruits)      # ['apple', 'banana', 'cherry', 'mango']

fruits[1] = "blueberry"  # update item
print(fruits)      # ['apple', 'blueberry', 'cherry', 'mango']

🔹 Dictionaries

A dictionary stores data as key–value pairs. Keys are unique, and values can be anything.

student = {"name": "Alice", "age": 20, "grade": "A"}

print(student["name"])    # Alice
student["age"] = 21       # update value
student["city"] = "Toronto"  # add new key-value
print(student)

👉 Output: {'name': 'Alice', 'age': 21, 'grade': 'A', 'city': 'Toronto'}


🔹 Tuples

A tuple is like a list, but it is immutable (cannot be changed once created). Tuples are often used for fixed collections of items.

coordinates = (10, 20)

print(coordinates[0])   # 10

# coordinates[0] = 15   # ❌ Error: Tuples cannot be changed

🔹 Sets

A set is an unordered collection of unique items. Sets are useful when you want to avoid duplicates.

numbers = {1, 2, 3, 3, 4}
print(numbers)   # {1, 2, 3, 4}

numbers.add(5)
print(numbers)   # {1, 2, 3, 4, 5}

numbers.remove(2)
print(numbers)   # {1, 3, 4, 5}

📝 Mini Task

Try this small exercise combining all four:

# Create a list of students
students = ["Alice", "Bob", "Charlie"]

# Convert it into a dictionary with grades
grades = {"Alice": "A", "Bob": "B", "Charlie": "C"}

# Store their ages in a tuple
ages = (20, 21, 19)

# Create a set of unique courses
courses = {"Math", "Science", "Math"}  # 'Math' will appear only once

print(students)
print(grades)
print(ages)
print(courses)

✅ Summary

  • Lists → ordered, changeable collections.
  • Dictionaries → key–value pairs for structured data.
  • Tuples → ordered but unchangeable collections.
  • Sets → unordered collections with unique items.

Understanding these four will give you the foundation to organize data effectively in Python.

Previous articleWorking with Files, APIs, and JSON in Python
Next articleSetting Up VS Code for Python Development
Heartin Kanikathottu
As a seasoned Cloud and Security Architect, I’ve led transformative initiatives in key roles, including Vice President at Morgan Stanley, Principal Architect at Societe Generale, and Tech Lead & Cloud Security Architect at VMware, among others. I’m also an internationally published author with multiple books available on platforms like Amazon and O'Reilly. Notably, one of my books was recognized as the 8th best cloud computing book of all time in 2020, reflecting the impact of my contributions to the field. With over 15 professional certifications from providers such as Microsoft (Azure), Amazon (AWS), Oracle (Java), Pivotal (Spring), and IBM, I bring a wealth of expertise to my work. Academically, I hold dual Master’s degrees in Cloud Computing and Data Analytics. I’m passionate about sharing knowledge and mentoring others, which is why I actively speak at global technical forums such as Tech Opportunities Fest at Platform Calgary, Google's Kubernetes Meetup, Java User Group, Elasticsearch Meetup, and the Agile India Conference.

LEAVE A REPLY

Please enter your comment!
Please enter your name here