Why isn't the zipped list printing?

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:

names = [‘Jenny’, ‘Alexus’, ‘Sam’, ‘Grace’]
dogs_names = [‘Elphonse’, ‘Dr. Doggy DDS’, ‘Carter’, ‘Ralph’]
#List of Lists
names_and_dogs_names = zip(names, dogs_names)
print(list(names_and_dogs_names))

Go back and carefully read and follow step two.

2 Likes

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:

names_and_dogs_names = list(zip(names, dogs_names))

It’s not needed for this exercise, but it was good for me to find out you can do this. Thanks for the help mtf!

2 Likes

What is the list() function actually doing that allows you to print out the list of lists? And why can’t you use print() without it?

there is a really simple way to find out, removing list:

names = ['Jenny', 'Alexus', 'Sam', 'Grace']
dogs_names = ['Elphonse', 'Dr. Doggy DDS', 'Carter', 'Ralph']
names_and_dogs_names = zip(names, dogs_names)

print(names_and_dogs_names)

so list() converts the zip object into a list.

1 Like

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?

Please help I am so confused

6 Likes

Please post your complete code in a reply.

1 Like
names = ['Jenny', 'Alexus', 'Sam', 'Grace']
dogs_names = ['Elphonse', 'Dr. Doggy DDS', 'Carter', 'Ralph']

names_and_dogs_names = zip(names, dogs_names)

list_of_names_and_dogs_names = list(names_and_dogs_names)

print(list_of_names_and_dogs_names)
print(list(names_and_dogs_names))
print(list_of_names_and_dogs_names)

This prints out the correct combined lists, then empty brackets, then the correct combined lists

names = [‘Jenny’, ‘Alexus’, ‘Sam’, ‘Grace’]
dogs_names = [‘Elphonse’, ‘Dr. Doggy DDS’, ‘Carter’, ‘Ralph’]

names_and_dogs_names = zip(names, dogs_names)

print(list(names_and_dogs_names))

list_of_names_and_dogs_names = list(names_and_dogs_names)

print(list_of_names_and_dogs_names)
print(list(names_and_dogs_names))
print(list_of_names_and_dogs_names)

This prints out the correct combined lists, then three rows of empty brackets

names = [‘Jenny’, ‘Alexus’, ‘Sam’, ‘Grace’]
dogs_names = [‘Elphonse’, ‘Dr. Doggy DDS’, ‘Carter’, ‘Ralph’]

names_and_dogs_names = zip(names, dogs_names)


list_of_names_and_dogs_names = list(names_and_dogs_names)

print(list(names_and_dogs_names))
print(list_of_names_and_dogs_names)

This prints out empty brackets and then the correct combined lists

2 Likes

zip returns an iterator, which once consumed cannot be re-consumed.

If you want to print multiple times, then use the list constructor on the zip object. Print the list.

The iterator is consumed in that line but the object can be reprinted.

list_of_names_and_dogs_names = list(names_and_dogs_names)
        reconsumable                      consumed
19 Likes

That is extremely helpful thank you SO much

1 Like

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.

Happy coding!

9 Likes

@mtf Did you find that article on Python iterators? If found, please care to share it. I’m willing to read that!!

Never did find it. Might just need to do a web search and hope to stumble on it.

can we go deep into this why so
and what is zip object

the zip object is an iterator of tuples.

There are good explanations on iterators and zip:

Iterator - Python Wiki

1 Like

I’m running into the same issue.

names = ['Jenny', 'Alexus', 'Sam', 'Grace']
dogs_names = ['Elphonse', 'Dr. Doggy DDS', 'Carter', 'Ralph']
names_and_dogs_names = zip(names, dogs_names)
list_of_names_and_dogs_names = list(names_and_dogs_names)
print(list_of_names_and_dogs_names)
print(list(names_and_dogs_names))

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.

That line is consuming the zip object. There is nothing left when you try to print it on the last line.

@gursewak_marahar: This one might help too (?) https://docs.python.org/3/c-api/list.html :slightly_smiling_face:

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.

https://printingnearby.com/