Lists

Creating a list in Python

my_list = [ ]
type(my_list)
my_list = [‘haha’, ‘100’, 100, 2.0]

To access element from a list you can choose each of them starting by element 0

my_list[0]
haha
my_list[3]
2.0

To change an element you go by selecting the index

my_list[0] = toto
print(my_list)
toto, 100, 100, 2.0

To add an element to the list

my_list.append(‘life is good’)
print(my_list)
toto, 100, 100, 2.0, life is good

my_list.extend([“what”, 999])
print(my_list)
toto, 100, 100, 2.0, life is good, what, 999

To remove an element to the list

my_list.pop(‘life is good’)
print(my_list)
toto, 100, 100, 2.0

my_list.pop() <— by doing this, it will always remove the last element of the list.

 

Leave a Comment