Using zip function multiple times

Hello,
Just a quick question that isn’t necessarily part of the exercise but should be simple to answer.

This is the link to the exercise i’m on but I’m trying to do a little extra.

Python Lists | Codecademy

first_names = ['Ainsley', 'Ben', 'Chani', 'Depak']

age = []
age.append(42)

all_ages = [32, 41, 29] + age

name_and_age = zip(first_names, all_ages)
print(list(name_and_age))

ids = range(4)
print(list(ids))

name_age_id = zip(name_and_age, ids)
print(list(name_age_id))

My issue lies at the bottom of the code. I was attempting to create a new variable, name_age_ids to combine ids with the list of name_and_age. However, when I run this, I end up with an just an empty bracket for the last print()

I actually just solved the problem by doing name_age_id = zip(first_names, all_ages, ids instead.

However, I am still posting this because I would like some insight if the original way I attempted it is possible, and if so what was I doing incorrectly?

This is because the name_and_age variable is a zip object, since you never actually convert the variable to list. ids is also not a list object; it’s a range object:

print(range(4))
#will print:
>>range(0, 4)
print(list(range(4)))
#will print:
>>[0, 1, 2, 3]

zip and range objects behave quite differently to list objects; one such difference is that when you zip them, there is nothing to zip.

1 Like

ah I understand now. Thank you!

1 Like