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!
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.
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.
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.
Just make a list of the result by doing list(zip(...))
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With