Creat python list


#1

Hi all,

I need to create a list that follows the following steps:

1 - create an empty list;
2 - Add the elements: 1, 2, 3, 4, 5, using 'append';
3 - Print the list;
4 - Remove the elements '3' and '6' (do not forget to check if they are in the list);
5 - Print the modified list;
6 - Print the size of the list using 'len';
7 - Change the value of the last element to '6' and print the modified list.

I managed to do it until step 3, but from 4 onwards I can not :frowning:

My code looks like this:

list = []
list.append(1)
list.append(2)
list.append(3)
list.append(4)
list.append(5)
print (list)

Can someone help me?


#2

We should avoid using reserved words or keywords as variable names. list is a class.

my_list = []
my_list.append(1)
my_list.append(2)
my_list.append(3)
my_list.append(4)
my_list.append(5)

print my_list    # [1, 2, 3, 4, 5]

To check if a value is in the list, use, in:

if 3 in my_list:
    my_list.remove(3)

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.