7. Lists
So far we have used variables to store a single value. Sometimes it is useful to store multiple values into a single variable using a list.
name_list = ["mary", "john", "paul"]
len(name_list)
>> 3You can add an item into a list using the append function
name_list.append("eric")
name_list
>> ['mary', 'john', 'paul','eric']
len(name_list)
>> 4Items in a list can be retrieved as follows:
name_list[0]
>> 'mary'
name_list[3]
>> 'eric'
name_list[0:2] #this retreives the first three elements (0, 1, 2) from the list
>> ['mary', 'john', 'paul']You can also perform different functions on a list, such as sorting, and finding maximum, minimum and length (number of items):
Last updated
Was this helpful?