FAQ: Creating and Modifying a List in Python - Review

This community-built FAQ covers the “Review” 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 Review

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!

2 posts were split to a new topic: Does age need to be at the beginning or end of all_ages?

ids = list(range(4))
name_and_age_ids = name_and_age + ids

prints: TypeError: unsupported operand type(s) for +: ‘zip’ and ‘list’

is there a way to add together a list and another list made with range?

Two lists can be concatenated to combine as one list. We cannot add a list to a range, so you are correct to cast it as a list, first.

Above the error message indicates you are attempting to concatenate a list with a zip object. This we cannot do. A zip object is an iterator, not a list.

can someone give me guidance. I’m not understanding why my code isn’t working.
i keep getting an error the following error "Did you combine age with the list [32, 41, 29] and store the result in all_ages ? all_ages should begin with 32, 41, 29 and end with 42".

… and ends with Depak’s age.

That means concatenate it to right side of the list, not the left. Your initial list should be empty. Even an empty string is still an element.

age = []

oh, thank you so much.

1 Like

Hello! I’ve recently started to learn Python and I would like someone to help me.

Could anyone tell me how to make a combined list that includes the information about ‘id’, ‘name’ and ‘age’ all together in one parenthesis? One element of the list would be like this; (0, ‘Ainsley’, 32). I tried to make the list by using zip(), but still no luck.

The three data sets need to be in their own lists.

>>> def foo():
    names = ['Ainsley', 'Bernard', 'Carl', 'Denise', 'Edgar', 'Fiona', 'Glenda']
    ages = [19, 20, 18, 21, 19, 21, 20]
    ids = [i for i, x in enumerate(names)]
    return list(zip(ids, names, ages))

>>> foo()
[(0, 'Ainsley', 19), (1, 'Bernard', 20), (2, 'Carl', 18), (3, 'Denise', 21), (4, 'Edgar', 19), (5, 'Fiona', 21), (6, 'Glenda', 20)]
>>> 
1 Like

Why do I have to change the type of both the zip and range to list when I want to print them, but I do not have to do change into a list type if I want to use them in a calculation, like here:

ids = range(4)
name_and_age_and_ids = zip(first_names, all_ages, ids)

OR

ids = range(4)
name_and_age = zip(first_names, all_ages)
name_and_age_and_ids = zip(name_and_age, ids)

Also, why is an additional pair of parenthesis added when printing the latter option from above?

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

age = []

age.append(42)

all_ages = [32, 41, 29] + age


ids = range(4)
name_and_age = zip(first_names, all_ages)
name_and_age_and_ids = zip(name_and_age, ids)


print(list(name_and_age_and_ids))

[((‘Ainsley’, 32), 0), ((‘Ben’, 41), 1), ((‘Chani’, 29), 2), ((‘Depak’, 42), 3)]

1 Like

A lot of python functions are designed to accept so-called iterable objects as arguments. Iterable includes both regular sequences like a list where every element is already stored in memory and certain objects like range which won’t necessarily store every single integer within that range in memory, instead it is set up to return each item individually. There functions effectively unpack such iterators for you.

On the other hand print just outputs the representation of an object, it is not designed to unpack an iterable itself. So a zip or range object will return some details about the object itself rather than the items that would be returned if it they were iterated through. A list on the other hand already exists in memory so it’s representation can easily show at least part of the contents of the list.

Apologies if that’s a bit vague but you’d need to know quite a bit about Python’s iterator and sequence objects before it’s clear.

Link below might help but search around if not-
https://towardsdatascience.com/iterables-and-iterators-in-python-849b1556ce27

1 Like

Thank you very much, it was very helpful and clear enough for a beginner!

1 Like

I was going back to review some things and I was wondering is there a way to take three lists and make them into a 2D list. I know I can create a 2D list with zip but that makes it a tuple right?

Heres my example from this review:

#I have three lists
first_names = ["Ainsley", "Ben", "Chani", "Depak"]

preferred_size = ["Small", "Large", "Medium", "Medium"]

shipping = [True, False, True, False]

Is there a way to make a 2D list that I can access like a 2D list

#I tried this but it says its a tuple 
new_list = list(zip(first_names, preferred_size, shipping)
print(new_list)

This gives me what I’m looking for but doesn’t allow me to access it like a list by doing something like this:

new_list[0][1] = "Medium"

Is there a way to do this?

There’s nothing in the base language to do this on a single line (that I can think of) but basic forms of iteration should be able to get your a list of lists without too much trouble. Loops should be introduced shortly in the course which would be a decent option (e.g. you loop through the zip and assign each iteration to a list).

1 Like

last_semester_gradebook = [[“politics”, 80], [“latin”, 96], [“dance”, 97], [“architecture”, 65]]

Your code below:

subjects=[“physics”, “calculus”, “poetry”, “history”]
grades=[98, 97, 85, 88]
gradebook=[[“physics”, 98], [“calculus”, 97], [“poetry”, 85], [“history”, 88]]
print(gradebook)
gradebook.append([“computer science”, 100])
print(gradebook)
gradebook.append([“visual arts”, 93])
print(gradebook)
gradebook[5][1]=98
gradebook[2][1].remove(85)

why is my .remove syntax wrong in the final line? Thank you

In truth, you would be the best one to tell us why. If you are willing to lay out your ideas, and accept criticism, then the solution will unfold that includes your contribution.

1 Like

I accessed the location when I indicated [2][1] and it’s saying that the location has no value. I do not know why it is wrong, that is why I posted it

gradebook[2][1] is not a list, so has no attribute, ‘remove’. To remove an element of the list, refer to it as gradebook[2], perhaps?

to my understanding, gradebook is a 2D list indicated by the brackets within brackets: gradebook=[[physics,98]]etc.

when I call gradebook.remove([2][1]) I am referencing [“poetry”, 85] in an attempt to remove the 85. It’s a list because of the square brackets and the comma that separates it, right? I defined gradebook as a list earlier in the code.
I got through the lesson because I used a different method, but I’m still confused on how that index is not within range/ there’s an error.

That reference is to 85.

gradebook[2]    =>  ['poetry', 85]