I am trying to print a list of tuples containing the size of each list from list of list
ex:
l=[1,3,4,5,6,7],[1,1],[1,9,9]
print(list(map(lambda x: ("sum",sum(1 for i in l)),l)))
-
The output is coming as [(‘sum’, 3), (‘sum’, 3), (‘sum’, 3)]
I am trying to get it using list comprehension.
-
The expected output I want should be
[(‘sum’,6),(‘sum’,2),(‘sum’,3)]
>Solution :
You have a bug, it should be
sum(1 for i in x) # x, not l
Also, len(x) would give you the same number.
The list-comprehension solution is
[('sum', len(x)) for x in l]