Zip/List - Python 3

So, I am doing the zip/list activity for python 3.0, and the shell prints what the instructions want, but it still says define the variable. I checked, double checked, and triple checked my spelling, and even copy and pasted the variable names, but it still says incorrect. Anyone know why?

Hello, @data8706205801, and welcome to the forums.

If you could post a link to the specific exercise, and paste a copy of your code following these guidelines, I’ll take a look.

here is the link
https://www.codecademy.com/courses/learn-python-3/lessons/create-python-list/exercises/zip
Here is the code

names = ['Jenny', 'Sam', 'Alexis']

dogs_names = ['Elphonse', 'Dr. Doggy DDS', 'Carter']

names_and_dogs_names = zip(names, dogs_names)

print(list(names_and_dogs_names))

screenchot

It looks like you edited the original lists. It also appears that the SCT for the exercise is expecting specific tuples in a specific order in the zip object. This is the expected order:

[('Jenny', 'Elphonse'), ('Alexus', 'Dr. Doggy DDS'), ('Sam', 'Carter'), ('Grace', 'Ralph')]

Your list has ‘Sam’ and ‘Alexus’ switched around, and ‘Grace’ & ‘Ralph’ are missing.
If changing the lists doesn’t correct the issue, include the SCT error message in another post.

Edit: Just noticed you also changed ‘Alexus’ to ‘Alexis’.

I changed to code to how you said, but it still gave me theAnnotation 2020-03-22 102150

Please post your code that produced that error.

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))

Are you getting the error from instruction 1 or 2? Instruction 2 asks us to create a new variable called list_of_names_and_dogs_names by calling list() on names_and_dogs_names as opposed to simply calling list() on the zip object, and printing the results. While it might seem trivial to have to create the additional variable, there’s actually a good reason to do so. For the context of this exercise, it isn’t necessarily relevant since the lesson ends with printing the list, but one caveat of a zip object is that it is a one-time iterable. Once a zip object has been iterated, it is spent so to speak. If the values aren’t saved, they are lost. If you were to try to print names_and_dogs_names a second time, you’d get an empty list:

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))
#add another print
print(list(names_and_dogs_names))

Output:

[('Jenny', 'Elphonse'), ('Alexus', 'Dr. Doggy DDS'), ('Sam', 'Carter'), ('Grace', 'Ralph')]
[]

If we save the results of calling list() on the zip object to a variable, however, we can access it as often as we 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_of_names_and_dogs_names)

Output:

[('Jenny', 'Elphonse'), ('Alexus', 'Dr. Doggy DDS'), ('Sam', 'Carter'), ('Grace', 'Ralph')]
[('Jenny', 'Elphonse'), ('Alexus', 'Dr. Doggy DDS'), ('Sam', 'Carter'), ('Grace', 'Ralph')]

I was having the problem with instruction 1.

Try deleting the print() statement. Your print(list(names_and_dogs_names)) iterates the zip object, so when the SCT tries to see if your zip object contains the correct values, it’s empty.
Hence this warning in the instruction:
Make sure to run the code for this step before proceeding to the next instruction!
Then second instruction is where you convert the zip object to a list, and print.

it worked, thank you!

1 Like