Tuple

A tuple is a finite ordered list of elements. Tuples are written by listing comma-separated elements within parentheses. Similar to lists, they are sequences. The main difference between tuples and lists is that tuples cannot be changed once assigned, whereas lists can. Tuples are occasionally written with square or angle brackets.

Most typed functional programming languages such as Lisp, Python, and Linda use tuples as product types. Uses for the tuple as a data type include passing a string of parameters from one program to another and representing a set of value attributes in a relational database. In some languages, tuples are embedded within other tuples within parentheses or brackets.

Some programming languages offer an alternative to tuples known as record types, which feature unordered elements accessed by label. Languages such as Haskell combine tuple product types and unordered record types into a single construct.

Python tuple

In Python, lists and tuples are two of the most commonly used data structures. In addition to the aforementioned difference of being changeable, other differences include:

  • Iterating over elements of a tuple is faster compared to iterating over a list.
  • In Python, elements of a tuple are enclosed in parentheses whereas elements of list are enclosed in square brackets.
  • If data needs to remain unchanged over time, a tuple is the preferred data type. If data may need to be changed in the future, a list should be used.

Python tuple example

A tuple can have any number of items that are of different types such as integer, float, and string. Here’s an example of a tuple in Python:

Program to run in Python:

# Different types of tuples

# Empty tuple
my_tuple = ()
print(my_tuple)

# Tuple having integers
my_tuple = (1, 2, 3)
print(my_tuple)

# tuple with mixed datatypes
my_tuple = (1, "Hello", 3.4)
print(my_tuple)

# nested tuple
my_tuple = ("mouse", [8, 4, 6], (1, 2, 3))
print(my_tuple)

Output:

()
(1, 2, 3)
(1, 'Hello', 3.4)
('mouse', [8, 4, 6], (1, 2, 3))

Webopedia Staff
Webopedia Staff
Since 1995, more than 100 tech experts and researchers have kept Webopedia’s definitions, articles, and study guides up to date. For more information on current editorial staff, please visit our About page.
Get the Free Newsletter
Subscribe to Daily Tech Insider for top news, trends & analysis
This email address is invalid.
Get the Free Newsletter
Subscribe to Daily Tech Insider for top news, trends & analysis
This email address is invalid.

Related Articles

Complete List of Cybersecurity Acronyms

Cybersecurity news and best practices are full of acronyms and abbreviations. Without understanding what each one means, it's difficult to comprehend the significance of...

Human Resources Management System

A Human Resources Management System (HRMS) is a software application that supports many functions of a company's Human Resources department, including benefits administration, payroll,...

How To Defend Yourself Against Identity Theft

Almost every worldwide government agency responsible for identity theft issues will tell you the same thing: The first step to fighting identity theft is...

Infographic

An infographic is a visual representation of information or data. It combines the words information and graphic and includes a collection of imagery, charts,...

ScalaHosting

ScalaHosting is a leading managed hosting provider that offers secure, scalable, and affordable...

HRIS

Human resources information system (HRIS) solutions help businesses manage multiple facets of their...

Best Managed Service Providers...

In today's business world, managed services are more critical than ever. They can...