How to create a list of tuples in Python

In this post, we’ll go through several different ways to make a list of tuples in Python. Lists are mutable, whereas tuples are immutable, which is a key distinction between these two types of data. What does this mean specifically? In other words, a list’s items can be edited or modified, but a tuple’s items cannot.

  • How to create a list of tuples in Python
  • How to create a list of tuples in Python by using the map() function
  • How to create a list of tuples in Python by using the list comprehension and tuple method
  • How to create a list of tuples in Python by using the zip method
  • Python Program to Create a list of tuples

How to create a list of tuples in Python

  • Here we will discuss how to create a list of tuples in Python.
  • In this example, we will simply create a list and tuple and display it. Like arrays, list objects are declared.
  • Since lists don’t always have to be homogeneous, they can store objects of several data types simultaneously. The items in the list are enclosed within square brackets [].
  • All the items (elements) must be enclosed in parenthesis (), each one separated by a comma, to form a tuple. Any number of objects may be of various types and may be included in a tuple (integer, float, list, string, etc.).

Example:

Let’s take an example and check How to create a list of tuples in Python

Source Code:

# Created list of tuple
Country_name = [(67, 'U.S.A'), (25, 'France'), (96, 'Germany'),
        (15, 'Australia'), (34, 'China')]
  
# Display the Content
print(" Creation list of tuple:",Country_name)

Above, we defined a variable called Country_name which holds a list of the country names along with integer numbers. The list is surrounded by brackets [] and tuples defined as enclosed parenthesis().

Here is the implementation of the following given code.

How to create a list of tuples in Python
Created tuples within the list in Python

This is how we created tuples of lists in Python.

Read: Python program for finding greatest of 3 numbers

How to create a list of tuples in Python by using the map() function

  • In this section, we will discuss how to create a list of tuples in Python by using the map() function. Map() is a built-in function in Python.
  • Python’s map() method applies a function to each item in an iterator that is provided as input. For instance, an iterator can return an iterable map object and it could be a list, tuple, set, dictionary, or string.
  • It is utilized when you want to alter each iterable element using a single transformation function. In Python, the iterable and function are provided as parameters to the map.

Syntax:

Let’s have a look at the Syntax and understand the working of the map() function in Python.

map(function, iterables)  
  • It consists of two main parameters
    • function: It is a function where a map provides each iterable item.
    • iterables: It is a sequence that you want to map.

Example:

Let’s take an example and check how to create a list of tuples in Python using the map() function.

Source Code:

# Creation list of tuples
Cities_in_USA = [["Phoenix"],["San Antonio"],["Dallas"],["San Jose"]]

# By using the map() function
new_output =list(map(tuple, Cities_in_USA ))

# Display the Content
print("Creation list of tuples :",new_output)

Here, Cities_in_USA is the input list to create a list of tuples. Using the map() function, we projected the given list to the tuple function. The mapped tuple values are then created as a list using the list() function.

You can refer to the below Screenshot.

How to create a list of tuples in Python by using the map function
Using the map function we created a list of tuples

As you can see in the Screenshot we have discussed how to create a list of tuples in Python by using the map() function.

Read: How to trim a string in Python

How to create a list of tuples in Python by using the list comprehension and tuple method

  • In this example, we will discuss How to create a list of tuples using the list comprehension and tuple method in Python.
  • Python’s list comprehension syntax makes it simple and efficient to create a list from a string. Executing a procedure on each item in the original list is a very concise technique to build a new list.
  • A list of tuples can be created using List Comprehension and the tuple() function in Python. The collection of elements given to the tuple() function contribute to the creation of tuples.

Syntax:

Here is the Syntax of the list comprehension method in Python.

[tuple(x) for x in inout_value]

Note: Here tuple(x) is an iterator to convert objects to tuples.

Example:

Let’s take an example and check how to create a list of tuples using the list comprehension and tuple method in Python.

Source Code:

# create a list with name
bikes_in_USA = [['Harley Davidson'], ['Confederate'], ['Motus MSTR'],
		['Alta Motors Redshift']]

# By using the list comprehension method
new_result = [tuple(x) for x in bikes_in_USA ]

# Display the Content
print(new_result)

In the following given code, first, we created the list within the list and assign the elements. Next, we used list comprehension and will iterate the items and by using the tuple() method it will be converted into a list of tuples.

Here is the execution of the following given code.

How to create a list of tuples in Python by using the list comprehension and tuple method
Created a list of tuples from the input nested list

This is how we can create a list of tuples from the input nested list.

Read: Python Extend Vs Append [Key Differences]

How to create a list of tuples in Python by using the zip method

  • Here we will discuss How to create a list of tuples using the zip method in Python.
  • The built-in zip() function in Python allows users to combine any number of iterables (list, str, etc.) by taking items from each iterable that is specified as input.
  • In this example, we will use the list method and within this, we passed the zip() function as an argument and this function will return an iterator of tuples.

Syntax:

Let’s have a look at the syntax and understand the working of the zip() function in Python.

zip(iterable1, iterable2, ...)

Note: This function only requires the iterable parameters and these iterator objects are combined together.

Example:

Let’s take an example and check How to create a list of tuples using the zip method in Python.

Source Code:

# Create two lists with state zipcode and state_name from U.S.A

State_Zipcode = [35004 , 90001, 	80001]
State_name = ['Alabama', 'California', 'Colorado']

# By using the zip() function
new_result = list(zip(State_Zipcode, State_name))

# Display the Content
print("Created list of tuple :",new_result)

In the above code, we first created two lists called State_Zipcode and State_name. Now we have to develop a list of tuples for this we used the list function and it would create a list of tuples by using the zip method and within this method, we passed the iterables as an argument. In this example, the iterables are listed.

Here is the Screenshot of the following given code

How to create a list of tuples in Python by using the zip method
Using the zip method to create a list of tuple

In this example, we have understood How to create a list of tuples by using the zip method in Python.

Read: Python program for binary search

Python Program to Create a list of tuples

  • In this section, we will discuss how to create a list of tuples with the first item as the number and the second item as the square of the number.
  • In this example, we will take a range and generates a list of tuples with the first item becoming the number and the second item being the number’s square as long as the range is within that range.
  • First, we will set the range of upper and lower values by taking the input user, and by using the list comprehension method we can easily create the list of tuples in this case first item will be the number, and the second item as the square of the number.

Example:

Let’s take an example and check how to create a list of tuples using the list comprehension method and taking input from the user.

Source Code:

new_lower_val=int(input("Enter the lower value:"))
new_higher_val=int(input("Enter the upper value:"))
# By using the list comprehension method
new_result=[(m,m**2) for m in range(new_lower_val,new_higher_val+1)]
print(new_result)

Here is the execution of the following given code.

Python Program to Create a list of tuples
Python Program to Create a list of tuples

You may also like to read the following Python tutorials.

In this article, we have discussed several different ways to make a list of tuples in Python. The following topic we have covered.

  • How to create a list of tuples in Python
  • How to create a list of tuples in Python by using the map() function
  • How to create a list of tuples in Python by using the list comprehension and tuple method
  • How to create a list of tuples in Python by using the zip method
  • Python Program to Create a list of tuples