How to Concatenate Two Lists in Python

Multiple data structures are available in Python to store the collection of data such as lists, set, dictionary, and tuple. The lists are mutable in nature and the tuples are immutable. To maximize the efficiency, or to avail the features of both tuple and list data structure, a list of tuples can be created in Python. 

This Python guide demonstrates the below-stated techniques to create a list of tuples in Python: 

Method 1: Understanding List of Tuples Through a Square Bracket

The below code demonstrates how to use the “square bracket list initializer” to create the list of tuples in Python:

Code:

tuple1 = (32, 23, 13)
tuple2 = ('Alex','Henry','John')
list_value = [tuple1, tuple2]
print(list_value)

In the above code:

  • Two tuples are created and stored in the variables “tuple1” and “tuple2”.
  • The tuple values are enclosed inside the square bracket using comma-separated syntax to create a list of tuples.

Output:

The above snippet verified that the list of tuples has been created.

Method 2: Using map() Function

In Python, the “map()” function is used along with the “list()” function to create the list of tuples, as shown in the below code snippet:

Code: 

list1 = [['Python'], ['Java'], ['Linux'], ['Ubuntu']]
list_tuple = list(map(tuple, list1))
print(list_tuple)

In the above code:

  • The list of lists is initialized in the program.
  • The “map()” function accepts the “tuple” as a first argument and the list variable as a second argument to return the new iterator.
  • The iterator is then passed inside the “list()” function to create the Python list of tuples.

Output:

The above snippet shows the newly created list of tuples.

Method 3: Using zip() Function

In the below code the “zip()” function is used along with the “list()” function to create the Python list of tuples:

Code:

list1 = [52, 23, 13]
list2 = ['Python', 'Java', 'Linux', 'Ubuntu']
list_of_tuple = list(zip(list1,list2))
print(list_of_tuple)

In the above code:

  • Two lists are initialized in the above program.
  • The “zip()” function accepts the multiple lists as an argument and returns the tuple iterator containing the list element.
  • The tuple value is then passed as an argument to the “list()” function to create the list of tuples.

Output:

The Python list of tuples has been successfully created using the “zip()” and “list()” functions.

Method 4: Using List Comprehension Method

The “List Comprehension” method is used with the “zip()” and “iter()” functions to create the Python list of tuples:

Code:

list1 = ['Python', 'Java', 'Linux', 'Ubuntu']
list_tuple = [i for i in zip(*[iter(list1)]*2)]
print(list_tuple)

In the above code:

  • The built-in zip() function is used along with “*” operator to convert the items of “list1” into tuples of 2 items each. 
  • To create an iterator the “iter()” function is used in the program.
  • First, the “*” operator unpacks the elements of the list and passes them as a separate parameter to the built-in zip() function.
  • The “zip()” function is used to combine the elements from each iterable passed as arguments into tuples and returns an iterator of tuples.
  • Finally, the result of the “zip” function is passed to the list comprehension to get the list of tuples.

Output:

The above code shows that the Python list of tuples has been created successfully.

Method 5: Using List Comprehension With tuple() Function

In the below code, the “List Comprehension” is used with the“tuple()” function to create the Python list of tuples:

Code: 

list1 = [['Python'], ['Java'], ['Linux'], ['Ubuntu']]
list_tuple = [tuple(x) for x in list1]
print(list_tuple)

In the above code:

  • The list comprehension method uses the “for loop” for iterating on the list and for each element the tuple() function creates the tuple.
  • The list comprehension method returns the list of tuples.

Output:

The above code shows that the Python list of tuples has been created successfully.

Conclusion

To create the list of tuples the square brackets “[]”, the “map()” function, the “zip()” function, and the List Comprehension are utilized in Python. The simplest and easiest method to create the list of tuples is enclosing the multiple tuple value inside the square bracket. Similarly, the “map()” and “zip()” functions can also be used to create a Python list of tuples. This Python post delivered an in-depth guide on how to create a Python list of tuples.