I’ve just been introduced to Zip (lesson 4 of Lists) and basically I can’t seem to get past the first instruction. The output I have is correct, as in it’s paired the two separate lists together into a a zip object which is then viewed by: print(list(names_and_dogs_names))
But I get an error message saying: " Is names_and_dogs_names a zip() with names first and dogs_names second?"
This is rather annoying because my printed output is correct, yet CodeAcademy compiler or whatever it is that checks that the condition for the instruction has bee met, has simply not worked. Unless there is something blatantly obvious I’ve missed out on. The full code is below:
So, it now works. I’m guessing because I just wrote this:
names_and_dogs_names = zip(names, dogs_names)
Wait… I’m an idiot. I just realized it. It’s because I hadn’t assigned the zip object to a list yet. And so my print statement wouldn’t have worked and I didn’t read the instruction properly. Which was basically just Run the code when you’ve created zip object, then follow onto instruction 2.
I’ve also came to learn that I can create a list and zip the object in one line like so:
Why does the program print empty brackets if I try to run a new print statement without using the variable that we created?
I completed the exercise correctly. Then when I type in print(list(names_and_dogs_names))
at the end I get an empty bracket
I have been playing with this for a while, and things like moving the print(list(names_and_dogs_names)) statement before creating the variable makes printing the variable come out as brackets.
I can’t find the pattern for why it sometimes prints out empty brackets and why it sometimes prints out the correct output of the combined lists.
I also get empty brackets every time i try to run print(list(names_and_dogs_names)) after the first time. However, if I run print(list_of_names_and_dogs_names) multiple times after we created the variable for instruction 2, it prints the correct output of the combined lists as many times as I want. Why does this happen?
You’re welcome. I’m trying to track down a post that contained a link to a very good article on Python iterators. Wish me luck.
Meanwhile, dig around in the official Python docs and Wikis, to get a basic gist of their usage and syntax, then explore whatever articles you can find online.
This is also a good time to sort of stop and smell the roses by reviewing everything you’ve learned about lists, and iterables in general. Once we iron out all the wrinkles in our knowledge base, it’s clear sailing moving on to bigger challenges.
returns only one list of names, followed by an empty list. Sorry to necropost, just trying to understand why that variable no longer contains usable info for the list function. Is the second print() function creating a new list with a variable that is now empty? I would have assumed that the variable would remain true unless indicated otherwise.
Python zip list of lists. Python zip list of lists defines three different lists with the same number of items and passes those lists to the zip() method, which will return the tuple iterator and then convert it into the list using the list() method.