Lists Flashcards
What is a list
A data structure that helps store and manipulate an ordered collection of items.
List could contain what types of data?
Int.
Float
Strings
Other lists
What are the common methods for working with lists?
append, insert, remove, pop, clear, index, count,sort
What are the two main ways to create a list?
- Square brackets
e.g x= [1,2] - List function
e.g x= list(1,2)
[1,2,3,4,5]
[0:4]
What will be the result and why
1234
The last item is not included when accessing list
How do you mutate a list
Specify the position you want to mutate along with the item
e.g x = [1,2,3,4]
x[2] = 7
This will give a new list such that;
x= 1,2,7,4
This differs from insert as insert only adds to the list.
List operators?
- Addition
- Multiplication
Note: lists can not be subtracted or divided
x=[1,2,3]
y=[a,b,c]
x+y
What will be the output
[1,2,3,’a’,’b’,’c’]
x=[1,2,3]
x*2
What will be the output
[1,2,3,1,2,3]
How do you use the methods in lists?
list_name.method(variable)
Some times positions may be required as in insert
List_name.method(index, variable)
Pop
Removes the item in a given position and return it. If no index is specified, it will remove the last item in the list
How do you sort in descending order?
list.sort(reverse = true)