# Python – Merge two lists into list of tuples

## Python | Merge two lists into list of tuples

Given two lists, write a Python program to merge the two lists into list of tuples.

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
Merge both the list into a list of tuple using a for loop. But the drawback is given two lists need to be of 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` `list1 ``=` `[``1``, ``2``, ``3``]` `list2 ``=` `[``'a'``, ``'b'``, ``'c'``]` `print``(merge(list1, list2))`

Output:

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

Approach #2 : Naive but more efficient
This method remove the above given drawback and work well with uneven lengths of the two lists. It also provide try catch error for Index error.

 `def` `merge(list1, list2):` `     ` `    ``merged_list ``=` `[]` `    ``for` `i ``in` `range``(``max``((``len``(list1), ``len``(list2)))):` ` ` `        ``while` `True``:` `            ``try``:` `                ``tup ``=` `(list1[i], list2[i])` `            ``except` `IndexError:` `                ``if` `len``(list1) > ``len``(list2):` `                    ``list2.append('')` `                    ``tup ``=` `(list1[i], list2[i])` `                ``elif` `len``(list1) < ``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))`

Output:

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

Approach #3 : Using `zip()`
using zip() method to merge the two list elements and then typecasting into 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))`

Output:

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

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

This method uses two for loops to enumerate through lists and merge 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))`

Output:

```[(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))`

Output:

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

Last Updated on March 17, 2022 by admin

## Inner Class in PythonInner Class in Python

Inner Class in Python A Python is an Object-Oriented Programming Language, everything in python is related to

## XGBoost for RegressionXGBoost for Regression

XGBoost for Regression The results of the regression problems are continuous or real values. Some

## Python – Get list of running processesPython – Get list of running processes

Python – Get list of running processes A Process is a program that is being executed (processed).

## Python PIL – ImageDraw.Draw.rectangle()Python PIL – ImageDraw.Draw.rectangle()

Python PIL | ImageDraw.Draw.rectangle() PIL is the Python Imaging Library which provides the python interpreter

## Get minimum values in rows or columns with their index position in Pandas-DataframeGet minimum values in rows or columns with their index position in Pandas-Dataframe

Get minimum values in rows or columns with their index position in Pandas-Dataframe Let’s discuss

## os.rename() method – Pythonos.rename() method – Python

Python | os.rename() method OS module in Python provides functions for interacting with the operating system.

## How tokenizing text, sentence, words works – NLPHow tokenizing text, sentence, words works – NLP

NLP | How tokenizing text, sentence, words works Natural Language Processing (NLP) is a subfield of

## Django Project MVT StructureDjango Project MVT Structure

Django Project MVT Structure Django is based on MVT (Model-View-Template) architecture. MVT is a software design pattern