Lists Flashcards
What are values inside a list called?
Elements
Rather than creating a variable for each new piece of data, we can collect related data inside a list using…
[ ] brackets
What’s the symbol for separating values in a list?
A comma ,
How many values can we store in a list?
As many as we’d like
Every element in a list has a numbered position called…
An Index
If there are four elements in a list, what is the number of the last index?
3 (because indices start from 0)
What do we call the position of an element in a list?
The element’s index
How do we update a value in a list?
We access the value by its index, then assign another value.
Which indices can we access in a list with two elements?
0 and 1
How do you add a value to the end of a list?
With append()
scores = [24, 23]
scores=.append(25)
print(scores)
[23, 24, 25]
Where does the added value appear when using .append()?
At the end of the list
How do we add a value to a specific index?
Use insert()
shopping = [“kiwis”, “peas”]
shopping.insert(0, lemon)
print(shopping)
[lemon, kiwis, peas]
What are the two parameters of a insert() function?
- The index where we want to insert the value
- The value to insert
When using append() or insert() how many elements can we add to a list?
One at a time
How can we remove the last element in a list?
Use pop() as printed here
How can we remove an element at a specific index?
Use pop() with the index between the ()
What two instructions can add values to a list?
.append()
.insert()
How can we save the value removed by .pop()?
By storing it in a variable
gifts = [“earrings”, “chocolates”]
removed = gifts.pop()
print(removed)
What’s an easy way to loop through all elements of a list?
Using a for loop
What is the configuration of a for loop?
The word “for”
A variable of our choice
The word “in”
Then the list name
Where is the variable in this for loop?
for artist in artists
The variable before in holds the value of the list element the loop is currently on.
Artist
How do we get the number of elements in a list?
By using the len() instruction.
users = [“Sarah”, “Mike”, “Ella”]
print(len(users))
What instruction do we use to get the number of elements in a list?
len()
How do you use the code max()?
To find the largest number in a list of data, code max() with the name of the list between the parentheses.