List Questions Flashcards

1
Q

a = [1,2,3,4,5,6,7,8]

a[1:4]

A

a = [1,2,3,4,5,6,7,8]
a[1:4]

returns [2,3,4]

indexes at 0 and does NOT include the last index

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

a = [1,2,3,4,5,6,7,8]

a[1:4:2]

A

a = [1,2,3,4,5,6,7,8]

a[1:4:2]

returns [2, 4]

slices along increments of 2 starting at your first index listed

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

a = [1,2,3,4,5,6,7,8]

a[ : : -1]

A

a = [1,2,3,4,5,6,7,8]

a[ : : -1]

returns [8, 7, 6, 5, 4, 3, 2, 1]

slices the list in reverse

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

a = [1, 2, 3, 4, 5]

sliceObj = slice(1, 3)

a[sliceObj]

A

a = [1, 2, 3, 4, 5]

sliceObj = slice(1, 3)

a[sliceObj]

return [2, 3]

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

lst = [x ** 2 for x in range (1, 11) if x % 2 == 1]

A

lst = [x ** 2 for x in range (1, 11) if x % 2 == 1]

x ** 2 is output expression,
range (1, 11) is input sequence,
x is variable and
if x % 2 == 1 is predicate part.

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

odd_square = [x ** 2 for x in range(1, 11) if x % 2 == 1]

print odd_square

A

odd_square = [x ** 2 for x in range(1, 11) if x % 2 == 1]
print odd_square

list contains square of all odd numbers from range 1 to 10

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

power_of_2 = [2 ** x for x in range(1, 9)]

print power_of_2

A

power_of_2 = [2 ** x for x in range(1, 9)]

print power_of_2

list contains power of 2 from 1 to 8

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

noprimes = [j for i in range(2, 8) for j in range(i*2, 50, i)]

primes = [x for x in range(2, 50) if x not in noprimes]

print primes

A

list contains prime and non-prime in range 1 to 50

noprimes = [j for i in range(2, 8) for j in range(i*2, 50, i)]

primes = [x for x in range(2, 50) if x not in noprimes]

print primes

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

print [x.lower() for x in [“A”, “B”, “C”]]

A

print [x.lower() for x in [“A”, “B”, “C”]]

list for lowering the characters

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

string = “my phone number is : 11122 !!”

print(“\nExtracted digits”)
numbers = [x for x in string if x.isdigit()]
print numbers

A

string = “my phone number is : 11122 !!”

print(“\nExtracted digits”)
numbers = [x for x in string if x.isdigit()]
print numbers

list which extracts number

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

a = 5
table = [[a, b, a * b] for b in range(1, 11)]
print(“\nMultiplication Table”)
for i in table:
print i

A

a = 5
table = [[a, b, a * b] for b in range(1, 11)]
print(“\nMultiplication Table”)
for i in table:
print i

A list of list for multiplication table

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

lst = filter(lambda x : x % 2 == 1, range(1, 20))

print lst

A

filtering odd numbers
lst = filter(lambda x : x % 2 == 1, range(1, 20))
print lst

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

We can use FILTER function to filter a list based on some condition provided as a ___________ as first argument and list as second argument

A

We can use FILTER function to filter a list based on some condition provided as a ___________ as first argument and list as second argument.

lambda expression

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

lst = filter(lambda x : x % 5 == 0,
[x ** 2 for x in range(1, 11) if x % 2 == 1])
print lst

A

filtering odd square which are divisble by 5
lst = filter(lambda x : x % 5 == 0,
[x ** 2 for x in range(1, 11) if x % 2 == 1])
print lst

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

lst = filter((lambda x: x < 0), range(-5,5))

print lst

A

filtering negative numbers
lst = filter((lambda x: x < 0), range(-5,5))
print lst

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

print reduce(lambda a,b: a if (a > b) else b, [7, 12, 45, 100, 15])

A

implementing max() function, using
print reduce(lambda a,b: a if (a > b) else b, [7, 12, 45, 100, 15])