I downloaded the jupyter notebook and have been working in vscode (as i have for all the ones you can do that up to now). In the comprehension section I added in an extra print line to make sure I got that cell to output correct from no.9… and now the code in no.10 is coming up blank, unless i comment/remove the print line.
My understanding was that the “list” function doesn’t change the original that is passed in and you have to store the result to keep the changes… but the addition of the print(list(…)) seems to be clearing the contents of the zip? but that doesn’t seem right what am I missing here?
When you use the built in function zip(), it combines (for lack of a better word) two lists & it returns a tuple, which is immutable. So, to print() it, you have to cast it as a list by using the built in list() function.
Lists are mutable.
And, in 10, you didn’t write the list comprehension quite right. (you’re close!). You’re creating a dictionary based on items in 2 lists…and matching the name to the age–one will be the key and the other is the value.
10:
Summary
names_to_ages = {names:ages for [names, ages] in zipped_ages}
print(names_to_ages)
Additionally, If it helps, you could also run your code (with that print(list()) part commented out and uncommented) through here: https://pythontutor.com/visualize.html#mode=edit
Which will give you another visual explanation too.
We can turn an iterable into an iterator in one fell swoop:
i = iter([1, 2, 3, 4])
while True:
print (next(i))
1
2
3
4
Traceback (most recent call last):
File "<pyshell#24>", line 2, in <module>
print (next(i))
StopIteration
Loops and Control Flow: That is what algorithms are based upon. Iterators are the way we funnel in data, top to bottom, or left to right, however one wants to see it.
Iterators are an optimization device in that they do not consume memory. The data consumes the memory. If we point an iterator to a block of data, it will not consume that memory. The data is safe. The iterator will only consume a virtual representation of the data, or should we say, realization since the true representation is withheld. We get it piecemeal, one item at a time. But the reference to that data point is forgotten.