Codecademy Forums

FAQ: Creating and Modifying a List in Python - Zip

This community-built FAQ covers the “Zip” exercise from the lesson “Creating and Modifying a List in Python”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science
Data Science

FAQs on the exercise Zip

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

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.

1 Like

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!

1 Like

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.

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

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

1 Like

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
1 Like

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!

1 Like

Hi,
I have been having difficulty with typing the code. I am literally doing names_and_dog_names = zip(names, dog_names). When I type in the code it gives me an error message-- when I copy and paste the results in-- the results are able to parse. Is there something that I am doing that I should be doing differently? What is it that I am doing wrong exactly? This is frustrating.

Unable to replicate issue. When making a question, please post full code so we can get the full picture, a single line of code is only a detail.

I think that my issue was that I was following the instructions but when I was using the zip function one of the other variables was not properly defined. Leading to an error saying that one of the functions was not defined.

I think some of the exercises were giving me that issue while going about it and just doing the code and nothing more.