# Python | Concatenate two lists into a list of tuples

| | |

👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!

Examples :

`  Input:  list1 = [1, 2, 3] list2 = [’a’,’ b’, ’c ’]  Output:  [(1,’ a’), (2, ’b’), (3,’ c’)]  Input:  list1 = [1, 2, 3, 4] list2 = [1, 4, 9]  Output:  [(1, 1), (2, 4), (3, 9), (4, ’’)] `

Approach # 1: Naive
Concatenate both lists into a list of tuples using a for loop. But the downside is that the two lists must be the same length.

 ` def ` ` merge (list1, list2) : ` ` merged_list ` ` = ` ` [(list1 [i], list2 [i]) ` ` for ` ` i ` ` in ` ` range ` ` (` ` 0 ` `, ` ` len ` ` (list1))] ` ` return ` ` merged_list ` ` # Driver code ` ` l ist1 ` ` = ` ` [` ` 1 ` `, ` ` 2 ` `, ` ` 3 ` `] ` ` list2 ` ` = ` ` [` ` ’a’ ` `, ` `’ b’ ` `, ` ` ’c’ ` `] ` ` print ` ` (merge (list1, list2)) `

Exit:

` [(1, ’a’), (2,’ b’), (3, ’c’)] `

Approach # 2: Naive but more effective
This method fixes the above drawback and works well with unequal lengths of two lists. This also provides a try catch error for the Index error.

 ` def ` ` merge (list1, list2): ` ` merged_list ` ` = ` ` [] ` ` for ` ` i ` ` in ` ` range ` ` (` ` max ` ` ((` ` len ` ` (list1), ` ` len ` ` (list2)))) : ` ` while ` ` T rue ` `: ` ` try ` `: ` ` tup ` ` = ` ` (list1 [i], list2 [i]) ` ` except ` ` IndexError: ` ` if ` ` len ` ` (list1) & gt; ` ` len ` ` (list2): ` ` list2.append (’’) ` ` tup ` ` = ` ` (list1 [i], list2 [i]) ` ` elif ` ` len ` ` (list1) & lt; ` ` len ` ` (list2): ` ` list1.append (’’) ` ` tup ` ` = ` ` (list1 [i], list2 [i]) ` ` continue ` ` merged_list.append (tup) ` ` break ` ` return ` ` merged_list ` ` # Driver code ` ` list1 ` ` = ` ` [` ` 1 ` `, ` ` 2 ` `, ` ` 3 ` `] ` ` list2 ` ` = ` ` [` ` ’a’ ` `, ` `’ b’ ` `, ` ` ’c’ ` `] ` ` print ` ` (merge (list1, list2)) `

Exit:

` [(1, ’a’), (2,’ b’), (3, ’c’)] `

Approach # 3 Using ` zip () `
using the zip () method to concatenate two list items and then inject them into a tuple.

 ` def ` ` merge (list1, list2): ` ` merged_list ` ` = ` ` tuple ` ` (` ` zip ` ` (list1, list2)) ` ` return ` ` merged_list ` ` # Driver code ` ` list1 ` ` = ` ` [` ` 1 ` `, ` ` 2 ` `, ` ` 3 ` `] ` ` list2 ` ` = ` ` [` ` ’a’ ` `, ` `’ b’ ` `, ` ` ’c’ ` `] ` ` print ` ` (merge (list1, list2)) `

Exit:

` ((1, ’a’), (2, ’b’), (3,’ c’)) `

Approach # 4: Using enumerate (), an alternative to zip ().

This method uses two for loops to iterate through lists and concatenate the two lists.

 ` def ` ` merge (list1 , list2): ` ` merged_list ` ` = ` ` [(p1, p2) ` ` for ` ` idx1, p1 ` ` in ` ` enumerate ` ` (list1) ` ` for idx2, p2 in enumerate (list2) if idx1 = = idx2] `` return merged_list # Driver code list1 = [ 1 , 2 , 3 ] list2 = [ ’a’ , ’ b’ , ’c’ ] print (merge (list1, list2)) `

Exit:

` [(1, ’a’), (2,’ b’) , (3, ’c’)] `

Approach # 5 Using ` map () ` and ` lambda `.

 ` # Using map () and lambda ` ` def ` ` listOfTuples (l1, l2): ` ` return ` ` list ` ` (` ` map ` ` (` ` lambda ` ` x, y: (x, y), l1, l2)) ` ` Driver code ` ` list1 ` ` = ` ` [` ` 1 ` `, ` ` 2 ` `, ` ` 3 ` `] ` ` list2 ` ` = ` ` [` ` ’a’ ` `, ` ` ’b’ ` `, ` `’ c’ ` `] ` ` print ` ` (listOfTuples (list1, list2)) `

Exit :

` [(1, ’a’), (2,’ b’), (3, ’c’)] `

👻 Read also: what is the best laptop for engineering students?

We hope this article has helped you to resolve the problem. Apart from Python | Concatenate two lists into a list of tuples, check other Loops-related topics.

Want to excel in Python? See our review of the best Python online courses 2022. If you are interested in Data Science, check also how to learn programming in R.

By the way, this material is also available in other languages:

Manuel Gonzalez

Prague | 2022-11-27

Maybe there are another answers? What Python | Concatenate two lists into a list of tuples exactly means?. Checked yesterday, it works!

Angelo Zelotti

Rome | 2022-11-27

Thanks for explaining! I was stuck with Python | Concatenate two lists into a list of tuples for some hours, finally got it done 🤗. I just hope that will not emerge anymore

Ken Ungerschaft

Prague | 2022-11-27

Simply put and clear. Thank you for sharing. Python | Concatenate two lists into a list of tuples and other issues with Python functions was always my weak point 😁. Will use it in my bachelor thesis

## Shop

Learn programming in R: courses

\$

Best Python online courses for 2022

\$

Best laptop for Fortnite

\$

Best laptop for Excel

\$

Best laptop for Solidworks

\$

Best laptop for Roblox

\$

Best computer for crypto mining

\$

Best laptop for Sims 4

\$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

NUMPYNUMPY

glob exclude pattern

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

NUMPYNUMPY

Python CSV error: line contains NULL byte

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

## Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically