L4 - Lists Flashcards
What is slicing?
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
What do you get with a negative element in list?
_ python counts from the end of the list backwards where -1 is the end of the list
What happens when you add two lists together?
- mergers the two lists
What does the split function to a string?
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 can you join strings together in a list?
What an asterisk between words
delimiter = ‘*’
delimiter.join(list_name)
What is the range function?
- 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]
What does list.append(x) do?
adds x to a list
What does list.insert(i, x) do?
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)
what do del list[i] do?
delete the object in that position of the list
equally you can do list.remove(x) –> to remove exactly all ‘x’ from the list
what does z = cities.pop() do?
removes the item from the list and creates a string ‘z’ now?
sorted(list)?
sorts the list
list.index(F)?
returns the index value of the string F in the list
What are the two ways you can compare each object in a list and return the index position of values that meet your criteria?
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