In: Computer Science
This program is about list comprehension.
list1 = [2, 5, 7, 8]
list2 = [1, 2]
(e) Use nested list comprehension with list1 and list2 as input sequences to generate this list:
[[3, 4], [6, 7], [8, 9], [9, 10]]
Display the list.
The following is the expected output.
Part e: [[3, 4], [6, 7], [8, 9], [9, 10]]
Code:
list1 = [2, 5, 7, 8]
list2 = [1, 2]
l=[]
for i in range(0,len(list1)):
a=[]
for j in range(0,len(list2)):
a.append(list1[i]+list2[j])
l.append(a)
print(l)
Code in image with explanation:
Output: