Originally published on CodingCush.
Python data structures are the built-in containers that hold groups of values in your program: lists for ordered, changeable collections; tuples for ordered, fixed collections; sets for unique values with no duplicates; and dictionaries for labelled values stored as key and value pairs. Choosing the right container makes your code shorter, faster and easier to read, because each structure is designed for a different job. This guide explains all four in plain words, with small examples you can type and run, and a simple rule for picking the right one every time.
Why Containers Matter at All
A single variable holds a single value, like one name or one price. Real programs juggle many values at once: all the students in a class, the items in a cart, the words in a sentence. Saving each one in its own variable would be like carrying groceries home one item per trip. Data structures are the bags and boxes: they carry many values together, each with its own shape and rules.
The official Python tutorial has a full chapter simply called Data Structures in the Python Tutorial, covering lists in depth, the del statement, tuples and sequences, sets, dictionaries and looping techniques. This guide follows the same ground in simpler words, so you can read the official chapter afterwards without fear.
Lists: Ordered and Changeable
fruits = ["apple", "banana", "mango"]
fruits.append("orange")
fruits[0] = "green apple"
print(fruits) # ['green apple', 'banana', 'mango', 'orange']
print(fruits[1]) # banana
A list keeps values in order, written inside square brackets, and you can change it after creating it: add, remove, replace or sort items freely. Positions are counted from zero, so the first item is index 0, a rule that surprises every beginner exactly once. Lists are the workhorse of Python: shopping carts, to-do items, search results and rows from a database all live naturally in lists. When order matters and the collection will grow or shrink, reach for a list first. Our detailed guide to Python list methods covers append, insert, remove, pop, sort and the rest with examples.
Tuples: Ordered but Fixed
point = (10, 20)
city = ("Delhi", 28.6, 77.2)
print(city[0]) # Delhi
# city[0] = "Jaipur" # Error: tuples cannot be changed
A tuple looks like a list written with round brackets, but once created it cannot be changed. That fixed nature is a feature, not a flaw. Tuples suit values that belong together and should stay together: coordinates, an RGB colour, a day-month-year date, or a database record returned as one row. Because a tuple cannot be edited by accident, it also works as a dictionary key, which a list cannot. A tuple with one item needs a trailing comma, like (5,), otherwise Python sees plain brackets around a number, a tiny trap worth knowing before it bites you.
The official Python Tutorial page "5. Data Structures" on docs.python.org, opening with "More on Lists" and the list methods append, extend, insert, remove and pop.
Sets: Unique Values, No Duplicates
tags = {"python", "coding", "python", "tutorial"}
print(tags) # {'python', 'coding', 'tutorial'} - duplicate gone
print("python" in tags) # True, and very fast
tags.add("beginners")
A set stores unique values only, written with curly brackets. Add the same value ten times and it is stored once, quietly, without an error. Sets do not promise a useful order, so you cannot ask for the third item; that is the trade you make. In return, checking whether a value exists in a set is extremely fast, and set maths is elegant: union joins two sets, intersection keeps common values, and difference shows what is in one but not the other. Sets shine for removing duplicates, checking membership, and comparing groups, like students enrolled in either of two courses. Think of a set as a guest list with a strict door: every name appears once, no matter how many times it was written down.
Dictionaries: Labelled Values with Keys
student = {"name": "Asha", "age": 21, "course": "Python"}
print(student["name"]) # Asha
student["marks"] = 88 # add a new pair
student["age"] = 22 # update a value
A dictionary stores pairs: a key that labels, and a value that belongs to it. Instead of remembering that age is "the second item", you simply ask for student["age"]. Dictionaries model real things beautifully: a student, a product, a user profile, or the settings of an app. Keys must be unique and unchangeable types like strings or numbers; values can be anything, including lists and other dictionaries. Our side-by-side guide to Python list vs dictionary shows exactly when indexed access beats labelled access, with the same data written both ways.
Lists, Tuples, Sets and Dictionaries Side by Side
| Structure | Written as | Ordered? | Changeable? | Duplicates? | Best for |
|---|---|---|---|---|---|
| List | [1, 2, 3] | Yes | Yes | Allowed | Collections that grow and change |
| Tuple | (1, 2, 3) | Yes | No | Allowed | Fixed groups that belong together |
| Set | {1, 2, 3} | No useful order | Yes | Removed automatically | Unique values and fast membership checks |
| Dictionary | {"a": 1} | By insertion | Yes | Keys unique, values may repeat | Labelled data about one thing |
The official Python documentation on built-in types is the complete reference for these containers, including every method and edge case, once the basics here feel comfortable.
A Simple Rule for Choosing
Ask three quick questions. Do the values need labels, like name and age? Choose a dictionary. Must duplicates disappear, or is fast "is it in there?" checking the main job? Choose a set. Should the group stay exactly as created, like coordinates? Choose a tuple. For everything else, the everyday growing, shrinking, ordered collection, choose a list. Most beginner programs live happily on lists and dictionaries alone for months, and there is no prize for using all four in every file, however tempting it feels.
Looping Through Each Structure
All four containers work with a plain for loop, but what the loop hands you differs, and that difference is worth seeing once, clearly. Looping a list or tuple gives you each value in order. Looping a set gives you each unique value, in no promised order. Looping a dictionary gives you its keys, and the common pattern student.items() gives you key and value together, which reads almost like English.
for key, value in student.items():
print(key, "=", value)
# name = Asha
# age = 22
# course = Python
Beginners often write loops that count positions by hand, like range(len(fruits)), when Python happily gives the items directly. If you ever need both the position and the value, enumerate() provides them cleanly. Simple loops over the container itself are shorter, clearer and harder to break, which is why experienced Python programmers reach for them first.
Frequently Asked Questions
What are the four main data structures in Python?
Lists, tuples, sets and dictionaries. Lists are ordered and changeable, tuples are ordered and fixed, sets hold unique values, and dictionaries store key and value pairs.
When should I use a tuple instead of a list?
Use a tuple when the group should never change after creation, such as coordinates, colours or a fixed record. Use a list when you plan to add, remove or edit items.
Can a dictionary have a list as a value?
Yes. Dictionary values can be any type, including lists, tuples, sets and other dictionaries. Only the keys must be unchangeable types such as strings, numbers or tuples.
How do I remove duplicates from a list?
Convert it to a set and back: unique = list(set(my_list)). This removes duplicates, but the order may change, so sort the result if order matters for your program.
Are Python data structures the same as DSA data structures?
They overlap but are not identical. These four are Python's built-in containers for daily coding. DSA as a subject also covers structures you build or study, like stacks, queues, trees and graphs, often using these built-ins as parts.
Conclusion
Lists carry, tuples protect, sets clean, and dictionaries label. Learn those four personalities and Python code becomes much easier to plan before you write it. Start with lists and dictionaries, add sets when duplicates annoy you, and use tuples when a group must stay fixed. The right container is a small decision that quietly makes everything after it simpler.

Top comments (0)