Python Tuples

Learn about Python tuples, a versatile data type for storing multiple items in a single variable. Tuples are ordered and immutable, making them ideal for fixed collections of data. Explore their characteristics and usage in this comprehensive guide.



Python Tuples

Tuples are used to store multiple items in a single variable. They are one of Python's four built-in data types for storing collections, alongside lists, sets, and dictionaries, each with its own unique characteristics and uses.

A tuple is an ordered and unchangeable collection. Tuples are written with round brackets.

Example

Create a Tuple:

Example

thistuple = ("orange", "grape", "mango")
print(thistuple)
Output

('orange', 'grape', 'mango')

Tuple Items

Tuple items are:

  • Ordered: The items have a defined order that does not change.
  • Unchangeable: Once a tuple is created, you cannot change, add, or remove items.
  • Allow Duplicates: Tuples can have multiple items with the same value.

Example

Tuples allow duplicate values:

Example

thistuple = ("orange", "grape", "mango", "orange", "mango")
print(thistuple)
Output

('orange', 'grape', 'mango', 'orange', 'mango')

Tuple Length

To determine how many items are in a tuple, use the len() function:

Example

Print the number of items in the tuple:


thistuple = ("orange", "grape", "mango")
print(len(thistuple))
Output

3

Create Tuple With One Item

To create a tuple with only one item, add a comma after the item; otherwise, Python will not recognize it as a tuple.

Example

One item tuple, remember the comma:


thistuple = ("orange",)
print(type(thistuple))
  
  # Not a tuple
thistuple = ("orange")
print(type(thistuple))
Output

<class 'tuple'>
<class 'str'>

Tuple Items - Data Types

Tuple items can be of any data type:

Example

String, int, and boolean data types:


tuple1 = ("orange", "grape", "mango")
tuple2 = (10, 20, 30, 40, 50)
tuple3 = (True, False, True)
print(tuple1)
print(tuple2)
print(tuple3)
Output

('orange', 'grape', 'mango')
(10, 20, 30, 40, 50)
(True, False, True)

A tuple can contain different data types:

Example

A tuple with strings, integers, and boolean values:


tuple1 = ("apple", 42, False, 29, "male")
print(tuple1)
Output

('apple', 42, False, 29, 'male')

The tuple() Constructor

It is also possible to use the tuple() constructor to create a tuple.

Example

Using the tuple() method to create a tuple:


thistuple = tuple(("orange", "grape", "mango")) # note the double round-brackets
print(thistuple)
Output

('orange', 'grape', 'mango')

Python Collections (Arrays)

There are four collection data types in the Python programming language:

  • List: An ordered and changeable collection that allows duplicate members.
  • Tuple: An ordered and unchangeable collection that allows duplicate members.
  • Set: An unordered, unchangeable*, and unindexed collection with no duplicate members.
  • Dictionary: An ordered** and changeable collection with no duplicate members.

*Set items are unchangeable, but you can remove and/or add items whenever you like.

**As of Python version 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries are unordered.

When choosing a collection type, it's essential to understand the properties of that type. Choosing the right type for a particular data set can preserve meaning and increase efficiency or security.