Python List of Tuples With 6 Examples

In this tutorial, you will learn about the Python List Of Tuples With Examples. I’ll go over what Tuples are in Python, what they’re used for, and how you can use them in your Python code.

What Is Python Tuples and List?

Python Tuple’s elements are enclosed within small parenthesis, and tuples are also immutable data structures in Python.

Syntax:

Example Of Tuples

tupleName = (item1, item2, item3..)

Where, 
      item = It can be any data type.

Whereas a List is a data structure that keeps an ordered collection of data elements. The list is a mutable data structure in nature and can be changed.

Syntax:

Examples Of List

listName = [item1, item2, item3..]
Where, 
      item = It can be any data type.

Python List Of Tuples (Normal Approach)

In order to create a list of tuples, we must first create a list in which the elements of the tuple are contained. This list will then have the same properties as a Python list. In light of the fact that Python Tuples take up less space than other data types, it is more practical to create a list of tuples in every case.

Let us see the code example for List Of Tuples in Python below.

#Initializing List Of Tuples In Python
listOfTuples = [(1,2,3), ("A", "B", "C")]

#Printing the List of Tuples
print(listOfTuples)

Output:

[(1, 2, 3), ('A', 'B', 'C')]

Python List Of Tuples Using zip() Function

The zip() function in Python can be used to map multiple lists together into a single list of tuples, as shown in the following command:

listName = list(zip(list))

Following the values passed to it, the zip() function returns an iterable of tuples based on the values passed to it. Furthermore, as an output from the zip() function, the list() function would generate a list of those tuples, which would then be passed to the list() function.

#Initializing List Of Tuples In Python
normalList1 = [1,2,3]
normalList2 = ["A", "B", "C"]

#Using zip() function to convert
#List Of Tuples
listOfTuples = list(zip(normalList1, normalList2))

#Printing the List of Tuples
print(listOfTuples)

Output:

[(1, 'A'), (2, 'B'), (3, 'C')]

Customized Element Grouping While Constructing A List Of Tuples

In the process of creating a list of tuples, it is possible for us to provide a customized grouping of elements based on the number of elements in the list/tuple we are forming. You can use the list comprehension method to achieve this.

Syntax:

listOfTupleName = [element for element in zip(*[iter(list)]*number)]

It is necessary to use list comprehension in conjunction with the zip() function in order to convert the tuples to lists and to create a list of tuples.

The iter() function in Python is used to iterate through an object one element at a time. The ‘number’ parameter would specify the number of elements that would be combined into a single tuple in order to form a list of elements.

#Initializing List Of Tuples In Python
normalList = [1,2,3,"A", "B", "C"]

#Using zip() function to convert
#List Of Tuples
listOfTuples = [item for item in zip(*[iter(normalList)])]

#Printing the List of Tuples
print(listOfTuples)

Output:

[(1,), (2,), (3,), ('A',), ('B',), ('C',)]

Using the iter() method, we have created a list of tuples with a single element contained within each tuple in the preceding example.

Let’s take a look at another code example in which we want to insert a three-element tuple into a list of items.

#Initializing List Of Tuples In Python
normalList = [1,2,3,"A", "B", "C"]

#Using zip() function to convert
#List Of Tuples
listOfTuples = [item for item in zip(*[iter(normalList)]*3)]

#Printing the List of Tuples
print(listOfTuples)

Output:

[(1, 2, 3), ('A', 'B', 'C')]

Python List Of Tuples Using Map

Create a list of tuples by using the map() function in the Python programming language. An iterable that has been passed to the map() function is mapped and applied by the map() function.

Let’s look at a Python code example of how to use the map function to get a list of Tuples.

#Initializing List Of Tuples In Python
normalList = [[1,2,3],["A", "B", "C"]]

#Using Map() function to convert
#List Of Tuples
listOfTuples = list(map(tuple, normalList))

#Printing the List of Tuples
print(listOfTuples)

Output:

[(1, 2, 3), ('A', 'B', 'C')]

Using the map() function, we mapped the input list to the tuple function in this example. Following that, the list() function is used to generate a list of the mapped tuple values.

Python List Of Tuples Using List Comprehension And Tuple() Method

To create a list of tuples, use Python’s tuple() method in conjunction with List Comprehension. The tuple() function assists in the creation of tuples from a set of elements passed to it.

Let us see the code example below for the usage of the tuple() function.

#Initializing List Of Tuples In Python
normalList = [[4,5,6],["D", "E", "F"]]

#Using tuple() function to convert
#List Of Tuples
listOfTuples = [tuple(item) for item in normalList]

#Printing the List of Tuples
print(listOfTuples)

Output:

[(4, 5, 6), ('D', 'E', 'F')]
Python list of tuples

Wrap Up

We’ve reached the end of the article now. The Python list of tuples is an intriguing concept, and I hope you’ve enjoyed learning about it. If you have any questions, feel free to ask them in the comments section.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. The “in” and “not in” operators in Python
  2. How to append an Array in Python
  3. Initialize A Python Array In 3 Ways
  4. Reverse an Array in Python

Leave a Comment