Python Reference Library →

Python tuple() Function – Create a Tuple from Iterable

Let’s look at tuple() function – create a tuple from iterable

  1. Notice

    tuple() constructs an immutable sequence from an iterable. Its elements can still refer to mutable objects.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
items = [3, 2]
values = tuple(items)
print(values)
Live state

Top-level scope

items→Object 1
values→Object 2
Object 1 · list[3, 2]Object 2 · tuple(3, 2)
This step

tuple() constructs an immutable sequence from an iterable. Its elements can still refer to mutable objects.

Picture tuple() function – create a tuple from iterable

tuple() constructs an immutable sequence from an iterable. Its elements can still refer to mutable objects.

Image brief

Alt text
A diagram for Python tuple() Function – Create a Tuple from Iterable. Instructions: items = [3, 2] values = tuple(items) print(values). Displayed result: (3, 2). The final example bindings are items --> object 1 (list); values --> object 2 (tuple). object 1: [3, 2]; object 2: (3, 2)
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python tuple() Function – Create a Tuple from Iterable". Teaching focus: tuple() constructs an immutable sequence from an iterable. Its elements can still refer to mutable objects. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS items = [3, 2] values = tuple(items) print(values) | v DISPLAYED RESULT (3, 2) FINAL BINDINGS items --> object 1 (list) values --> object 2 (tuple) object 1: [3, 2] object 2: (3, 2) Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python tuple() Function – Create a Tuple from Iterable. Instructions: items = [3, 2] values = tuple(items) print(values). Displayed result: (3, 2). The final example bindings are items --> object 1 (list); values --> object 2 (tuple). object 1: [3, 2]; object 2: (3, 2)

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
items = [3, 2]
values = tuple(items)
print(values)
Live state

Top-level scope

items→Object 1
values→Object 2
Object 1 · list[3, 2]Object 2 · tuple(3, 2)
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
(3, 2)

What do you expect this program to print?

What does this example display?

items = [3, 2]
values = tuple(items)
print(values)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

(3, 2)

Follow the printed values in order. The output is: (3, 2)

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

tuple() constructs an immutable sequence from an iterable. Its elements can still refer to mutable objects.

tuple() constructs an immutable sequence from an iterable. Its elements can still refer to mutable objects.

More examples and reference

Python tuple() Function

The tuple() function in Python is used to create an immutable ordered collection of elements called a tuple. Tuples are similar to lists, but they cannot be changed after creation.

Syntax

tuple(iterable)

Parameters:

  • iterable – (Optional) Any iterable like a list, string, set, or dictionary.

Returns:

  • A new tuple containing elements from the given iterable.

Example 1: Convert a List to Tuple

my_list = [1, 2, 3]
result = tuple(my_list)
print(result)
(1, 2, 3)

Example 2: Convert a String to Tuple

text = "abc"
result = tuple(text)
print(result)
('a', 'b', 'c')

Example 3: Using tuple() with a Dictionary

my_dict = {'a': 1, 'b': 2}
result = tuple(my_dict)
print(result)
('a', 'b')

Note: Only the keys are included in the resulting tuple.

Example 4: Create an Empty Tuple

empty = tuple()
print(empty)
()

Why Use Tuples?

  • Immutability: Tuples can’t be modified, making them useful for fixed data.
  • Performance: Faster than lists for read-only data.
  • Hashable: Tuples can be used as dictionary keys or set elements (if they only contain hashable items).

Common Mistakes

  • Writing tuple = () and then using tuple() will cause an error (do not overwrite built-ins).
  • Confusing parentheses () with tuple creation – remember, () alone is empty tuple, (5) is just a number, use (5,) to create a one-element tuple.

Interview Tip

In interviews, tuples are often used when returning multiple values from functions, or for storing coordinates, RGB values, etc.

Summary

  • tuple() converts an iterable into a tuple.
  • Tuples are immutable – you can't change them after creation.
  • Empty tuple: tuple(); one-item tuple: (item,).

Practice Problem

Convert the following list into a tuple, and then try to change one value. What happens?

numbers = [10, 20, 30]
t = tuple(numbers)
print(t)
# Try this:
# t[0] = 99  # What error do you get?

What to remember

Connect what you noticed in the example with the idea below.

Remember

tuple() constructs an immutable sequence from an iterable. Its elements can still refer to mutable objects.

Watch out for this idea

A tuple guarantees that every object it contains is immutable.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →