Last updated: Apr 9, 2024
Reading timeยท3 min
To print a zipped list:
zip()
function to get an iterator of tuples.list()
class to convert the iterator to a list.print()
function to print the list.list_1 = [1, 2, 3] list_2 = ['bobby', 'hadz', 'com'] my_list = list(zip(list_1, list_2)) # ๐๏ธ [(1, 'bobby'), (2, 'hadz'), (3, 'com')] print(my_list)
The zip() function iterates over several iterables in parallel and produces tuples with an item from each iterable.
The zip
function returns an iterator of tuples.
list_1 = [1, 2, 3] list_2 = ['bobby', 'hadz', 'com'] zip_obj = zip(list_1, list_2) # ๐๏ธ <zip object at 0x7fb7621fe080> print(zip_obj)
This is why we had to use the list()
class to convert the zip
object to a
list.
list_1 = [1, 2, 3] list_2 = ['bobby', 'hadz', 'com'] zip_obj = list(zip(list_1, list_2)) # ๐๏ธ [(1, 'bobby'), (2, 'hadz'), (3, 'com')] print(zip_obj)
You can use the zip()
function with as many iterables as necessary.
list_1 = [1, 2, 3] list_2 = ['bobby', 'hadz', 'com'] list_3 = ['one', 'two', 'three'] zip_obj = list(zip(list_1, list_2, list_3)) # ๐๏ธ [(1, 'bobby', 'one'), (2, 'hadz', 'two'), (3, 'com', 'three')] print(zip_obj)
The zip()
function returns a zip
object of tuples.
To get list output instead of tuple from the zip()
function:
zip
object.list()
class to convert each tuple to a list.list_1 = [1, 2, 3] list_2 = ['a', 'b', 'c'] list_of_lists = [list(tup) for tup in zip(list_1, list_2)] # ๐๏ธ [[1, 'a'], [2, 'b'], [3, 'c']] print(list_of_lists)
The zip() function iterates over several iterables in parallel and produces tuples with an item from each iterable.
By default, the zip
function returns an iterator of tuples.
list_1 = [1, 2, 3] list_2 = ['a', 'b', 'c'] # ๐๏ธ [(1, 'a'), (2, 'b'), (3, 'c')] print(list(zip(list_1, list_2)))
We used a
list comprehension to
iterate over the zip
object.
On each iteration, we used the list()
class to convert the tuple to a list.
list_1 = [1, 2, 3] list_2 = ['a', 'b', 'c'] list_of_lists = [list(tup) for tup in zip(list_1, list_2)] # ๐๏ธ [[1, 'a'], [2, 'b'], [3, 'c']] print(list_of_lists)
The list class takes an iterable and returns a list object.
Alternatively, you can use the map()
function.
This is a two-step process:
map()
function to pass each tuple in the zip
object to the
list()
class.list()
class to convert the map
object to a list.list_1 = [1, 2, 3] list_2 = ['a', 'b', 'c'] list_of_lists = list(map(list, zip(list_1, list_2))) # ๐๏ธ [[1, 'a'], [2, 'b'], [3, 'c']] print(list_of_lists)
The map()
function takes a function and an iterable as arguments and calls the
function with each item of the iterable.
map()
function passes each tuple in the zip
object to the list()
class.The map()
function returns a map
object, so we had to use the list()
class
to convert it to a list.
Which approach you pick is a matter of personal preference. I'd go with using a list comprehension because I find it more explicit and easier to read.
You can learn more about the related topics by checking out the following tutorials: