Python - 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])
17
Q

“in” operator

A

“in” operator: This operator is used to check if an element is present in the list or not. Returns true if element is present in list else returns false.

ex) lis = [1, 4, 3, 2, 5]
if 4 in lis:
print ….

18
Q

“not in” operator

A

“not in” operator : This operator is used to check if an element is not present in the list or not. Returns true if element is not present in list else returns false.

ex) lis = [1, 4, 3, 2, 5]
if 4 not in lis:
print ….

19
Q

len()

A

len() : This function returns the length of list.

20
Q

min()

A

min() :- This function returns the minimum element of list.

21
Q

max()

A

max() :- This function returns the maximum element of list.

22
Q

+ operator

A

“+” operator :- This operator is used to concatenate two lists into a single list.

23
Q
  • operator
A

“*” operator :- This operator is used to multiply the list “n” times and return the single list.

24
Q

index(ele, beg, end)

A

index(ele, beg, end) :- This function returns the index of first occurrence of element after beg and before end.

# using index() to print first occurrence of 3
# prints 5
print ("The first occurrence of 3 after 3rd position is : ", end="")
print (lis.index(3, 3, 6))
25
Q

count()

A

count() :- This function counts the number of occurrences of elements in list.

# using count() to count number of occurrence of 3
print ("The number of occurrences of 3 is : ", end="")
print (lis.count(3))
26
Q

del[a : b]

A

del[a : b] :- This method deletes all the elements in range starting from index ‘a’ till ‘b’ mentioned in arguments.

27
Q

pop()

A

pop(): This method deletes the element at the position mentioned in its arguments.

28
Q

insert(a, x)

A

insert(a, x) :- This function inserts an element at the position mentioned in its arguments. It takes 2 arguments, position and element to be added at respective position.

29
Q

remove()

A

remove() :- This function is used to delete the first occurrence of number mentioned in its arguments.

30
Q

sort()

A

sort() :- This function sorts the list in increasing order.

31
Q

reverse()

A

reverse() :- This function reverses the elements of list.

32
Q

extend(b)

A

extend(b) :- This function is used to extend the list with the elements present in another list. This function takes another list as its argument.

33
Q

clear()

A

clear() :- This function is used to erase all the elements of list. After this operation, list becomes empty.