L4 - Lists Flashcards

1
Q

What is slicing?

A

It a of returning only a part of a list

cities[2:4} –> where the first number is the first element of the list you want to appear and the second is the first element of the list you don’t want to appear anymore

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

What do you get with a negative element in list?

A

_ python counts from the end of the list backwards where -1 is the end of the list

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

What happens when you add two lists together?

A
  • mergers the two lists
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does the split function to a string?

A

s = ‘to be or not to be’

  • s.split(‘ ‘) = default of split() is the spaces anyways
  • creates a list [‘To’, ‘be’, ‘or’, ‘not’, ‘to’, ‘be’]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How can you join strings together in a list?

A

What an asterisk between words

delimiter = ‘*’

delimiter.join(list_name)

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

What is the range function?

A
  • iterable function that will return its iterable values if called upon
    • range(2,10)
    • list(range(2,10))
      • [2,3,4,5,6,7,8,9]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does list.append(x) do?

A

adds x to a list

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

What does list.insert(i, x) do?

A

inserts the variable x into the list in the ith position

if written list.append(2,y) –> y will replace the second variable position (so the third one in)

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

what do del list[i] do?

A

delete the object in that position of the list

equally you can do list.remove(x) –> to remove exactly all ‘x’ from the list

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

what does z = cities.pop() do?

A

removes the item from the list and creates a string ‘z’ now?

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

sorted(list)?

A

sorts the list

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

list.index(F)?

A

returns the index value of the string F in the list

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

What are the two ways you can compare each object in a list and return the index position of values that meet your criteria?

A

using a counter

def top_salaries(salaries, threshold):
count = -1
result = []
for i in salaries:
count = count + 1
if i >= threshold:
result.append(count)
return result
“””

using range

def top_salaries(salaries, threshold):
result = []
for i in range(len(salaries)):
if salaries[i] >= threshold:
result.append(i)
return result

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