Question | Click to View Answer |
What does the following code print? numbers = [
[10, 20],
[30, 40],
]
result = 0
for i in numbers:
for j in i:
result = result + j
print(result)
|
This code uses nested Run this code snippet to better understand how nested for i in numbers:
print("outer loop")
print(i)
for j in i:
print("inner loop")
print(j)
|
What does the following code print? animals = [
{"species": "dog", "is_mammal": True},
{"species": "fish", "has_hair": "False"},
]
result = []
for a in animals:
result.append(a["species"])
print(result)
|
A |
What does the following code print? from functools import reduce
nums = [
[1, 2, 13, 15],
[30, 40],
]
res = []
for i in nums:
r = reduce(lambda x, y: x + y, i)
res.append(r)
print(res)
|
A |
What does the following code print? letters = [
["a", "b", "c", "d"],
["e", "f"],
]
res = []
for i in letters:
for j in i:
res.append(j)
print(res)
|
The nested loop flattens the nested list data structure into a single list. |
What does the following code print? stuff = [
["aa", "bbb", "c", "d"],
["eeeee", "ff"],
]
biggest = ""
for i in stuff:
for j in i:
biggest = biggest if len(biggest) > len(j) else j
print(biggest)
|
The A nested loop is used to iterate the nested data structure and continuously update the |
What does the following code print? fun_numbers = [
[8, 13, 17],
[98, 99],
]
is_odd = lambda y: y % 2 != 0
maybe = map(lambda b : list(filter(is_odd, b)), fun_numbers)
print(list(maybe))
|
We iterate through the Nesting |