Lists Flashcards
What are lists? How are they formed?
Lists store an ordered collection of data, the form is:
[val1, val2,…]. The values can be of any type (strings, int, float etc) and can consist of multiple types in one list.
Values are closed in square brackets and separated by commas
Can lists be sliced and indexed? If so how?
Yes they can. They are sliced and indexed just like strings.
grades = [34, 54, 76, 88, 97]
print(grades[0])
34
print(grades[ : :-2])
97, 76, 34
What are nested lists? How can they be used and accessed?
Nested lists are lists within a list. A value can be accessed in a nested list by indexing.
students_and_grades=[ [Sodaba, 87, 35, 99, 54], [John, 44, 66, 78, 99] }
print(students_and_grades[1][2]
66
Are lists mutable? If so how?
Yes they are. To change a value in a list, simply reference the index and set it equal to something else.
grade = [44, 66, 88, 33 ,56, 24, 97]
grades[-2] = 44
print(grades)
[44, 66, 88, 33, 56, 44, 97]
What are some built in list operators and how do you use them?
min(list) = minimum value of the list, if strings, prints by order of alphabet max(list) = self explanatory lol len(list) = returns the length of the list (count starts at 1) sum(list) = returns the sum of items in the list, value must be numeric
What is aliasing? How can you avoid it?
When two variable names refer to the same object. This happens when making copies of lists. If one variable is modified than the other is as well (refer to lists slides)
To avoid this , we can slice all items of the list and assign it to a new variable or nest the list.
lista = some list listb = list(lista)
What are the methods for adding objects and lists to a lists and removing objects?
adding = use .append( ) method
adding list = use .extend( ) method
removing = use . remove( ) method
How do you loop through a list? How do you loop through a nested list?
1 - regular for loop
for i in list_name
2 - nested for loop
for i in list_name
for j in i
How do you loop through a list? How do you loop through a nested list?
1 - regular for loop
for i in list_name
print(i)
this prints every item in a list
2 - nested for loop for row in list_name for column in list row print(column) this prints every single item in a nest. If you only want to access each list in the large list, don't do the column part.