Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the "old" zip() in Python3?

I migrated from Python 2.7 to Python 3.3 and zip() does not work as expected anymore. Indeed, I read in the doc that it now returns an iterator instead of a list.

So, how I am supposed to deal with this? Can I use the "old" zip() in my Python3 code?

Find bellow the way it worked before in a Django project:

in views.py: my_zipped_list = zip(list1, list2)

in file.html: {{ my_zipped_list.0.1 }}

Maybe another solution would be to keep "new" zip() behaviour and change template instead.

Thanks for help!

like image 214
David D. Avatar asked Sep 28 '14 22:09

David D.


People also ask

What is zip () in Python?

Python zip() Function The zip() function returns a zip object, which is an iterator of tuples where the first item in each passed iterator is paired together, and then the second item in each passed iterator are paired together etc.

What does zip (* list do in Python?

Python's zip() function is defined as zip(*iterables) . The function takes in iterables as arguments and returns an iterator. This iterator generates a series of tuples containing elements from each iterable. zip() can accept any type of iterable, such as files, lists, tuples, dictionaries, sets, and so on.

How do you print a zip object in Python?

If you want to include unmatched characters from the other two strings in the zipped object, use zip_longest() function defined in itertools module. Instead of None , any other character can be specified as fillvalue parameter. print(list(itertools.


1 Answers

Just make a list of the result by doing list(zip(...)).

like image 115
BrenBarn Avatar answered Sep 21 '22 00:09

BrenBarn