IT Career Guide
Search

Python List vs Tuple vs Set vs Dictionary: Differences with Examples

Python code on a laptop screen

Use a list for an ordered, changeable sequence, a tuple for a fixed sequence that should not change, a set for unique items and fast membership checks, and a dictionary for key-value lookups. That is the short verdict on Python list vs tuple vs set vs dictionary. This guide explains each one with code, compares them side by side and shows how to choose the right one in real programs and interviews.

Key takeaways

  • Lists and dictionaries are mutable, tuples are immutable, and sets are mutable but hold only unique, hashable items.
  • Lists and tuples keep order and allow duplicates, while sets remove duplicates and do not support indexing.
  • Dictionaries keep insertion order in Python 3.7 and later, and their keys must be unique and hashable.
  • Checking membership with in is usually much faster on a set or dictionary than on a list, especially for large collections.
  • Tuples can be used as dictionary keys or set items because they are hashable when all their elements are hashable.

Python list vs tuple vs set vs dictionary at a glance

FeatureListTupleSetDictionary
Syntax[1, 2, 3](1, 2, 3){1, 2, 3}{‘a’: 1, ‘b’: 2}
MutableYesNoYesYes
OrderedYesYesNoYes, insertion order
DuplicatesAllowedAllowedNot allowedKeys unique, values can repeat
IndexingBy positionBy positionNot supportedBy key
Membership testSlower, checks items one by oneSlower, checks items one by oneFast, hash lookupFast on keys
Typical useCollections that changeFixed records, return valuesUnique items, set mathsLookups by name or id

Python list

A list is an ordered, mutable collection. You can add, remove and change items, and it can hold mixed types.

marks = [78, 92, 85]
marks.append(90)        # [78, 92, 85, 90]
marks[0] = 80           # [80, 92, 85, 90]
marks.sort()            # [80, 85, 90, 92]
print(marks[-1])        # 92

When to use a list

  • The number of items changes, such as a shopping cart.
  • Order matters and you need indexing or slicing.
  • Duplicates are meaningful, such as daily temperature readings.

Python tuple

A tuple is an ordered, immutable collection. Once created, you cannot add, remove or replace its items.

point = (12.97, 77.59)   # latitude, longitude
lat, lon = point         # tuple unpacking
print(lat)               # 12.97
# point[0] = 13.0  would raise TypeError

A tuple with one item needs a trailing comma, for example (5,), otherwise Python treats it as a number in brackets.

When to use a tuple

  • The data is a fixed record, such as coordinates or an RGB colour.
  • You want to return several values from a function.
  • You need a dictionary key made of several values.

Python set

A set is an unordered collection of unique items. It is ideal for removing duplicates and for set operations such as union and intersection.

cities = {'Pune', 'Mumbai', 'Pune', 'Delhi'}
print(cities)                 # {'Pune', 'Mumbai', 'Delhi'} in some order
cities.add('Chennai')
print('Pune' in cities)       # True

a = {1, 2, 3}
b = {2, 3, 4}
print(a | b)   # union {1, 2, 3, 4}
print(a & b)   # intersection {2, 3}
print(a - b)   # difference {1}

Note that empty curly brackets {} create an empty dictionary, not a set. Use set() for an empty set.

When to use a set

  • You need only unique values, such as unique visitor ids.
  • You check membership often in a large collection.
  • You compare two groups, for example students enrolled in both courses.

Python dictionary

A dictionary stores key-value pairs. You look up values by key instead of by position.

student = {'name': 'Asha', 'course': 'Python', 'marks': 88}
print(student['name'])              # Asha
student['marks'] = 91               # update
student['city'] = 'Nagpur'          # add
print(student.get('phone', 'NA'))   # NA, no KeyError
for key, value in student.items():
    print(key, value)

When to use a dictionary

  • You look up data by a name, id or code.
  • You count things, for example word frequencies.
  • You represent structured data such as a JSON response from an API.

Performance: why set and dictionary lookups are fast

Sets and dictionaries use hash tables. To check if an item exists, Python computes its hash and jumps almost directly to the right place, so the check takes roughly constant time on average. A list has to compare items one by one, so the time grows with its length. For a list of a few items this difference does not matter, but for thousands of lookups on large data it matters a lot.

ids_list = list(range(1_000_000))
ids_set = set(ids_list)
print(999_999 in ids_list)  # scans the list
print(999_999 in ids_set)   # hash lookup, much faster

Mutability and hashability explained simply

Mutable means you can change the object after creating it. Hashable means Python can compute a stable hash for it, which is required for set items and dictionary keys. Immutable built-in types such as int, str and tuple (of hashable items) are hashable. Lists, sets and dictionaries are mutable and not hashable, so they cannot be dictionary keys.

How to choose: a quick decision guide

  1. Do you need to look up values by a key? Use a dictionary.
  2. Do you need only unique values or fast membership checks? Use a set.
  3. Is the data fixed and should not change? Use a tuple.
  4. Otherwise, for an ordered collection that changes, use a list.

This comparison is a favourite in fresher interviews, so practise explaining it aloud. Our Python interview questions include follow-up questions, and you can apply these data structures in Python projects for beginners. New to the language? Start with learn Python for beginners.

Frequently asked questions

Which is faster, list or tuple?

Tuples are slightly faster to create and use a little less memory because they are immutable. For most programs the difference is small, so choose based on whether the data should change.

Can a set contain a list?

No. Set items must be hashable, and lists are not. You can store a tuple instead, as long as the tuple contains only hashable items.

Is a dictionary ordered in Python?

Yes. Since Python 3.7, dictionaries officially keep items in insertion order.

How do I convert between list, tuple and set?

Use the built-in constructors: list(x), tuple(x) and set(x). Converting a list to a set and back is a quick way to remove duplicates, though it does not keep the original order.

Explore more guides in our Python section.

Other articles

Radium4 — IT training blog

Practical, plain-English guides for DevOps, cloud, data, Python, testing and security learners.