Categories
Python Answers

How to merge lists into a list of tuples with Python?

Spread the love

Sometimes, we want to merge lists into a list of tuples with Python.

In this article, we’ll look at how to merge lists into a list of tuples with Python.

How to merge lists into a list of tuples with Python?

To merge lists into a list of tuples with Python, we can use the list and zip functions.

For instance, we write

list_a = [1, 2, 3, 4]
list_b = [5, 6, 7, 8]
zipped = list(zip(list_a, list_b))

to call zip with list_a and list_b to combine the entries in the 2 lists into an iterable of tuples with the items from each list in the same position.

Then we use list to convert the zipped iterable into a list.

Conclusion

To merge lists into a list of tuples with Python, we can use the list and zip functions.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *