Rates TodayNIFTY25,670-0.83%SENSEX83,630.7-0.83%Gold 24K ₹11,320.24/g down186.46Silver ₹134.77/g down2.72
InsightforGeeks
PythonPart of the Python learning hub

Python Lists, Tuples, Dictionaries and Sets: When to Use Each

insightforgeeks-editorial
2 Dec 2026
3 min read
Share:

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 both

How to choose

You needUseWhy
An ordered collection that changeslistAdd, remove and sort items
A fixed group of valuestupleCannot be changed by accident, and can be a dictionary key
Lookup by a name or IDdictionaryFinds a value by key almost instantly
Unique values or "is it in here?"setNo 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.

Tags:#python#list#tuple#dictionary#set#data structures
Found this helpful? Share it with friends & colleagues
Share:
insightforgeeks-editorial Verified Editorial Staff

Covering financial markets, commodity rates, and practical technology insights for India. Researched and fact-checked in accordance with our Editorial Policy.

Published on 2 December 2026General Information · Disclaimer
More in Python