Lists Flashcards

1
Q

What are lists? How are they formed?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Can lists be sliced and indexed? If so how?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are nested lists? How can they be used and accessed?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Are lists mutable? If so how?

A

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]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are some built in list operators and how do you use them?

A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is aliasing? How can you avoid it?

A

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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the methods for adding objects and lists to a lists and removing objects?

A

adding = use .append( ) method
adding list = use .extend( ) method
removing = use . remove( ) method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you loop through a list? How do you loop through a nested list?

A

1 - regular for loop
for i in list_name

2 - nested for loop

for i in list_name
for j in i

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you loop through a list? How do you loop through a nested list?

A

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly