Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Python Dictionary

  1. Home
  2. Python Dictionary
  3. list() / tuple() / set()

list() / tuple() / set()

Since: Python 2(2000)

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

FunctionDescription
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

list_tuple_set_1.py
# Use list() to convert various objects to a list.
print(list(range(5)))
print(list("python"))
print(list((1, 2, 3)))
print(list({1, 2, 3}))

# Create a list of sorcerer names.
sorcerers = ("Gojo Satoru", "Itadori Yuji", "Fushiguro Megumi", "Kugisaki Nobara", "Nanami Kento")
sorcerer_list = list(sorcerers)
print(sorcerer_list)
print(type(sorcerer_list))

Running the code produces the following output:

python3 list_tuple_set_1.py
[0, 1, 2, 3, 4]
['p', 'y', 't', 'h', 'o', 'n']
[1, 2, 3]
[1, 2, 3]
['Gojo Satoru', 'Itadori Yuji', 'Fushiguro Megumi', 'Kugisaki Nobara', 'Nanami Kento']
<class 'list'>
list_tuple_set_2.py
# Use tuple() to convert a list to a tuple.
# Tuples are immutable, making them suitable for constant data.
grades = [1, 2, 3, 4, 5]
t = tuple(grades)
print(t)
print(type(t))

# Tuples can be used as dictionary keys (lists cannot).
locations = {
    (35.68, 139.76): "Tokyo",
    (34.69, 135.50): "Osaka",
}
print(locations[(35.68, 139.76)])

# Store a sorcerer's attributes as a fixed tuple.
gojo = ("Gojo Satoru", "Special Grade Sorcerer", "Infinity")
print(gojo[0], "technique:", gojo[2])

Running the code produces the following output:

python3 list_tuple_set_2.py
(1, 2, 3, 4, 5)
<class 'tuple'>
Tokyo
Gojo Satoru technique: Infinity
list_tuple_set_3.py
# Use set() to remove duplicates.
encountered = ["Gojo Satoru", "Itadori Yuji", "Gojo Satoru", "Fushiguro Megumi", "Itadori Yuji"]
unique = set(encountered)
print(unique)

# Use set operations for mathematical set arithmetic.
grade1 = {"Gojo Satoru", "Nanami Kento", "Fushiguro Megumi"}
grade2 = {"Fushiguro Megumi", "Kugisaki Nobara", "Itadori Yuji"}
print(grade1 & grade2)
print(grade1 | grade2)
print(grade1 - grade2)

# Use frozenset() to create an immutable set.
forbidden = frozenset(["Culling Game", "Reversed Cursed Technique"])
access_table = {forbidden: "Special Grade Only"}
print(access_table[forbidden])

Running the code produces the following output:

python3 list_tuple_set_3.py
{'Gojo Satoru', 'Itadori Yuji', 'Fushiguro Megumi'}
{'Fushiguro Megumi'}
{'Gojo Satoru', 'Nanami Kento', 'Fushiguro Megumi', 'Kugisaki Nobara', 'Itadori Yuji'}
{'Gojo Satoru', 'Nanami Kento'}
Special Grade Only

Common Mistakes

Common Mistake 1: Trying to modify a tuple

Tuples are immutable. Attempting to change, add, or remove elements raises a TypeError. If you need to modify the data, use a list, or convert the tuple to a list first.

mistake1_ng.py
sorcerers = ("Gojo Satoru", "Itadori Yuji", "Fushiguro Megumi")
sorcerers[0] = "Nanami Kento"

Running the code produces the following output:

python3 mistake1_ng.py
TypeError: 'tuple' object does not support item assignment
mistake1_ok.py
sorcerers = ("Gojo Satoru", "Itadori Yuji", "Fushiguro Megumi")
sorcerer_list = list(sorcerers)
sorcerer_list[0] = "Nanami Kento"
sorcerers = tuple(sorcerer_list)
print(sorcerers)

Running the code produces the following output:

python3 mistake1_ok.py
('Nanami Kento', 'Itadori Yuji', 'Fushiguro Megumi')

Common Mistake 2: Forgetting that set() changes element order

Sets do not preserve order. To remove duplicates while keeping the original order, use list(dict.fromkeys(iterable)), which works because dictionaries maintain insertion order in Python 3.7 and later.

mistake2_ng.py
names = ["Itadori Yuji", "Gojo Satoru", "Itadori Yuji", "Fushiguro Megumi"]
deduped = list(set(names))
print(deduped)

Running the code produces the following output:

python3 mistake2_ng.py
['Fushiguro Megumi', 'Gojo Satoru', 'Itadori Yuji']
mistake2_ok.py
names = ["Itadori Yuji", "Gojo Satoru", "Itadori Yuji", "Fushiguro Megumi"]
deduped = list(dict.fromkeys(names))
print(deduped)

Running the code produces the following output:

python3 mistake2_ok.py
['Itadori Yuji', 'Gojo Satoru', 'Fushiguro Megumi']

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 — can be stored as a tuple to prevent modification. 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, converting the list to a set before searching is faster.

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 .