Almost every Python program is built from four containers: lists, tuples, dictionaries and sets. They look similar, but each is good at something different. This guide shows what each one does, with code you can run, and how to choose.
Lists: an ordered collection you can change
A list keeps items in order and lets you add, remove and replace them. Use it when order matters and the contents change.
fruits = ["apple", "banana", "cherry"]
fruits.append("mango") # add to the end
fruits[1] = "blueberry" # replace by position
fruits.remove("apple") # remove by value
print(fruits) # ['blueberry', 'cherry', 'mango']
print(fruits[0], fruits[-1]) # blueberry mango
print(fruits[1:]) # ['cherry', 'mango']Positions start at 0, and negative numbers count from the end. The [1:] form is a slice: everything from position 1 onwards.
Tuples: a fixed group of values
A tuple looks like a list but cannot be changed after it is created. Use it for values that belong together and should not change, such as a point on a map or a row returned from a database.
point = (12.97, 77.59) lat, lon = point # unpack into two names print(lat, lon) # 12.97 77.59 # point[0] = 1 # would raise TypeError: tuples cannot be changed
Dictionaries: look things up by name
A dictionary stores key-value pairs and finds a value by its key very quickly. Use it whenever you would otherwise search a list for a name or an ID. Since Python 3.7, dictionaries remember the order in which keys were added.
user = {"name": "Asha", "city": "Kochi"}
user["age"] = 30 # add a new key
print(user["name"]) # Asha
print(user.get("phone", "none")) # none (no error if the key is missing)
for key, value in user.items():
print(key, "=", value)Use .get() when a key might be missing. Writing user["phone"] directly would raise a KeyError.
Sets: unique items and fast membership checks
A set holds each value only once and has no order. Use it to remove duplicates or to check quickly whether something is present.
visitors = ["a", "b", "a", "c", "b"]
unique = set(visitors)
print(sorted(unique)) # ['a', 'b', 'c']
subscribed = {"a", "c"}
print("b" in subscribed) # False
print(sorted(unique & subscribed)) # ['a', 'c'] items in bothHow to choose
| You need | Use | Why |
|---|---|---|
| An ordered collection that changes | list | Add, remove and sort items |
| A fixed group of values | tuple | Cannot be changed by accident, and can be a dictionary key |
| Lookup by a name or ID | dictionary | Finds a value by key almost instantly |
| Unique values or "is it in here?" | set | No duplicates, very fast membership tests |
Speed: why membership tests differ
Checking x in my_list looks at items one by one, so it gets slower as the list grows. Checking x in my_set or x in my_dict uses hashing and stays fast however big the container is. If you test membership often on a large collection, convert the list to a set first.
A quick tip: comprehensions
Each container has a short form for building it from another collection:
numbers = [1, 2, 3, 4, 5, 6]
evens = [n for n in numbers if n % 2 == 0]
squares = {n: n * n for n in numbers}
print(evens) # [2, 4, 6]
print(squares) # {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36}Next, learn to read and write files to store these structures, or set up a clean project with a virtual environment.
Covering financial markets, commodity rates, and practical technology insights for India. Researched and fact-checked in accordance with our Editorial Policy.
