python Flashcards
what method will you use to add 1 to the end of the list l = [1,2,3,4]?
l.append(1)
[1,2,3,4,1]
what method will you use to add 1 to position 1 of the list l = [1,2,3,4]
l.insert(1, 1)
[1,1,2,3,4]
what method will you use to add [1,2,3] to the end of the list l = [1,2,3,4]
l.extend([1,2,3])
[1,2,3,4,1,2,3]
how would you access the 4th element in the list l = [1,2,3,4,5]
l[4] or l[-1]
5
how would you remove 4 in the list l = [1,2,3,4,5]
l.remove(4)
[1,2,3,5]
how would you remove the last and the 2nd element in the list l = [1,2,3,4,5]
l. pop()
l. pop(2)
how would you sort the list l = [1,2,3,4,5] in asc order?
l.sort()
how would you sort the list l = [1,2,3,4,5] in desc order?
l.sort(reverse=True)
how would you reverse a list of items l = [1,2,3,4,5]?
l.reverse()
how would you multiply all elements of a list = [1,2,3,4,5]?
import functools
functools.reduce(lambda a, b: a*b, list)
how would you add all the numbers in a list = [1,2,3,4,5]?
sum(1,2,3,4,5)
how would you contain only odd numbers of the list = [1,2,3,4,5]?
l = [1,2,3,4,5]
filter(lambda x: x % 2, l)
[1,3,5]
how would you copy a list l = [1,2,3,4,5]
copy(l)
how would you access first element of a dictionary dict = {‘name’:’noor’, ‘age’:27}
dict[‘name’]
how would you delete age from the dictionary dict = {name: noor, age: 27}
del dict[‘age’]