list() / tuple() / set()
Built-in functions that convert between lists, tuples, and sets. Choose the appropriate type based on the characteristics of each data type.
Syntax
# Converts an iterable to a list. list(iterable) # Converts an iterable to a tuple (immutable list). tuple(iterable) # Converts an iterable to a set (no duplicates, unordered). set(iterable) # Converts an iterable to an immutable set. frozenset(iterable)
Function List
| Function | Description |
|---|---|
| list(iterable) | Converts an iterable to a list. Called with no arguments, it creates an empty list. |
| tuple(iterable) | Converts an iterable to a tuple. Tuples are immutable — their contents cannot be changed after creation. |
| set(iterable) | Converts an iterable to a set. Duplicates are removed automatically, and order is not guaranteed. |
| frozenset(iterable) | Creates an immutable set. Can be used as dictionary keys or as elements of another set. |
Sample Code
# Use list() to convert various objects to a list.
print(list(range(5))) # [0, 1, 2, 3, 4]
print(list("python")) # ['p', 'y', 't', 'h', 'o', 'n']
print(list((1, 2, 3))) # [1, 2, 3] (tuple → list)
print(list({1, 2, 3})) # [1, 2, 3] (set → list)
# Use tuple() to convert a list to a tuple.
nums = [1, 2, 3, 4, 5]
t = tuple(nums)
print(t) # (1, 2, 3, 4, 5)
print(type(t)) # <class 'tuple'>
# Tuples are immutable.
# t[0] = 99 # Raises TypeError.
# Tuples can be used as dictionary keys (lists cannot).
coordinates = {(35.68, 139.76): "Tokyo", (34.69, 135.50): "Osaka"}
print(coordinates[(35.68, 139.76)]) # Tokyo
# Use set() to remove duplicates.
nums_with_dup = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
unique = set(nums_with_dup)
print(unique) # {1, 2, 3, 4} (order may vary)
# Remove duplicates with a set, then convert back to a list.
deduped = list(set(nums_with_dup))
print(sorted(deduped)) # [1, 2, 3, 4]
# Set operations for mathematical set arithmetic.
a = {1, 2, 3, 4, 5}
b = {3, 4, 5, 6, 7}
print(a & b) # {3, 4, 5} (intersection — elements in both)
print(a | b) # {1, 2, 3, 4, 5, 6, 7} (union)
print(a - b) # {1, 2} (difference — in a but not in b)
print(a ^ b) # {1, 2, 6, 7} (symmetric difference — in one but not both)
# Use frozenset() to create an immutable set.
fs = frozenset([1, 2, 3])
print(fs) # frozenset({1, 2, 3})
# fs.add(4) # AttributeError — frozenset cannot be modified.
# frozenset can be used as a dictionary key or as a set element.
permissions = frozenset(["read", "write"])
access_table = {permissions: "standard user"}
# Summary of each type's characteristics.
# list → ordered, allows duplicates, mutable
# tuple → ordered, allows duplicates, immutable
# set → unordered, no duplicates, mutable
# frozenset → unordered, no duplicates, immutable
Notes
Lists, tuples, and sets each serve different purposes. Data that does not need to change — such as coordinates, RGB values, or days of the week — is safer to store as a tuple. Tuples are also slightly more memory-efficient than lists and are hashable, making them usable as dictionary keys.
The primary uses of a set are deduplication and fast membership testing. The in operator on a set runs in O(1) on average, compared to O(n) for a list. When you need to check whether a value exists in a large collection of data frequently, convert the list to a set before searching.
Converting to a set does not preserve element order. If you need to remove duplicates while keeping the original order, you can use list(dict.fromkeys(iterable)), which works because dictionaries maintain insertion order in Python 3.7 and later. For set-based dictionary operations, see also dict.keys() / values() / items().
If you find any errors or copyright issues, please contact us.