The above code is a Python list comprehension that adds corresponding elements of sublists in the nested list lis. Let's break it down:
lis = [[8, 7], [6, 5]]
result = [p + q for p, q in lis]
print(result)
lis is a nested list with two sublists: [8, 7] and [6, 5].
The list comprehension [p + q for p, q in lis] iterates over each sublist, and for each sublist, it takes elements p and q and calculates their sum p + q.
The result is a new list (result) containing the sums of corresponding elements from the sublists.
When you print the result, you'll get:
[15, 11]
This is because:
For the first sublist [8, 7], the sum of corresponding elements is 8 + 7 = 15.
For the second sublist [6, 5], the sum of corresponding elements is 6 + 5 = 11.
So, the final output is [15, 11].
0 Comments:
Post a Comment