python Flashcards

1
Q

what method will you use to add 1 to the end of the list l = [1,2,3,4]?

A

l.append(1)

[1,2,3,4,1]

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

what method will you use to add 1 to position 1 of the list l = [1,2,3,4]

A

l.insert(1, 1)

[1,1,2,3,4]

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

what method will you use to add [1,2,3] to the end of the list l = [1,2,3,4]

A

l.extend([1,2,3])

[1,2,3,4,1,2,3]

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

how would you access the 4th element in the list l = [1,2,3,4,5]

A

l[4] or l[-1]

5

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

how would you remove 4 in the list l = [1,2,3,4,5]

A

l.remove(4)

[1,2,3,5]

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

how would you remove the last and the 2nd element in the list l = [1,2,3,4,5]

A

l. pop()

l. pop(2)

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

how would you sort the list l = [1,2,3,4,5] in asc order?

A

l.sort()

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

how would you sort the list l = [1,2,3,4,5] in desc order?

A

l.sort(reverse=True)

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

how would you reverse a list of items l = [1,2,3,4,5]?

A

l.reverse()

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

how would you multiply all elements of a list = [1,2,3,4,5]?

A

import functools

functools.reduce(lambda a, b: a*b, list)

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

how would you add all the numbers in a list = [1,2,3,4,5]?

A

sum(1,2,3,4,5)

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

how would you contain only odd numbers of the list = [1,2,3,4,5]?

A

l = [1,2,3,4,5]
filter(lambda x: x % 2, l)
[1,3,5]

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

how would you copy a list l = [1,2,3,4,5]

A

copy(l)

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

how would you access first element of a dictionary dict = {‘name’:’noor’, ‘age’:27}

A

dict[‘name’]

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

how would you delete age from the dictionary dict = {name: noor, age: 27}

A

del dict[‘age’]

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

how would you delete the dictionary dict = {name: noor, age:27}

A

del dict