How can I edit an existing list variable to add a new item?

Question

For this exercise, how can I edit the existing list variable in the code to add new data items?

Answer

A list is an ordered collection of items. Lists are enclosed using the square brackets [] and each item is separated in the list using a comma. To edit an existing list, go the inside of the closing bracket ] and type a comma and then the new item to add to the list. To add multiple items, repeat the process by adding another comma and a new item but do not go beyond the closing square bracket of the list.

my_list = [1, 2, 3, 4]

# Edit the list in place. The new list will look like this:
my_list = [ 1, 2, 3, 4, 5]
9 Likes

I really went the long way round on this one. I figured writing it out that way would be cheating. I couldn’t remember Codecademy teaching me how to add something to a list. I tried ‘+=’ addition assignment; of course that didn’t work. Finally I went to w3schools and got the following solution:

heights = [61, 70, 67, 64]
heights.append(65)
print(heights)
63 Likes

Nothing wrong with using documentation if you need to find the name of the function or method which you want to use

4 Likes

Yooo! Very cool this command. Very helpful. Thank you very much :upside_down_face:

One could also use the += operator to add new list items.
heights = [61, 70, 67, 64, 65]
heights += [78]
print (heights)
This will print [61, 70, 67, 64, 65, 78]

13 Likes

+= worked just fine for me.

heights = [61, 70, 67, 64]
heights += [65]
print(heights)

returns

[61, 70, 67, 64, 65]

4 Likes

Why not just do height+= ["the new item to add]? We have already learnt this and can just start applying.

But it worked
In [68]: heights = [61, 70, 67, 64]

In [69]: heights += [65]

In [70]: heights
Out[70]: [61, 70, 67, 64, 65]

However, i will still rather go with your solution.
heights = [61, 70, 67, 64]
heights.append(65)
print(heights)

1 Like

append() was specifically designed to add an item to the list, list extending also has it usage. So you need to know both, so you can the right approach for the problem you are facing

7 Likes

Yep, love w3school and their Python Tutorial as well!)

Hey, quick question regarding this exercise. Assuming I was I was working with a long list of elements, say, 1000 instead of just 4 in broken_heights. Manually separating the elements in the list with commas would be laborious. Would it be possible to use a for loop to accomplish this?

I would need to the lesson, can you provide the url for the lesson? The exercise url at the top of this topic doesn’t work for me

Hi stetim94,

Here is the code. Let me know if it helps.

What determines that values are broken heights? Sure, we could write loop + logic to automate this task, but then you would need some kind of requirement.

In the commented line, the elements are broken because they are not comma-separated. My grasp of python is still very tenuous but I tried doing this…

…and ran into a invalid syntax error.

Kindly assist.

Can you please also provide me the exercise url/link? I really need to see the instructions

A list needs to have certain syntax, you can’t just violate syntax, that results in a syntax error.

you could make the broken heights into a string:

broken_heights = "65 71 59 62"

that would work.

But what determines that a height is broken? We can write code to generate broken heights, but then there needs to be a reason for heights to be broken.

You can use

# Add a single item to the end
my_list.append(5)  # faster, but in-place
my_list.add(5) 

my_list = my_list + [5]  # concatenate: slower

# Add many items at the end
my_list.extend([5,6,7])  # faster, but in-place

Made this in while loop for inserting 1000 values in list. Cheers

list1 = []
x = len(list1)
while x < 1000:
    v = int(input('Enter values'))
    list1.append(v)
    x += 1
print(list1)
1 Like

yes right , but they mentioned in later step.

this also works with creating sublist. I tried the append() and it works flawlessly. Thanks!