Reputation: 5804
I'd like to produce the sum for each sublist and append it right back onto the sublist.
LIST = [[1,1],[2,2],[3,3],[4,4]]
Desired Output:
[[1,1,2],[2,2,4],[3,3,6],[4,4,8]]]
My non-ideal Work-Around is creating a new list based on LIST and then zip them together after.
Sum = map(lambda x: sum(x), LIST)
zip(List,Sum)
Result
[([1, 1], 2),([2, 2], 4),([3, 3], 6),([4, 4], 8)]
This will be done on a huge list, so trying to be as efficient as possible
Upvotes: 1
Views: 86
Reputation: 1
There are different solutions to be applied in your case for producing sum of sublists and appending it like an element and they can vary in performance.
Tests have been done for 10000 element list with 10000 repeat under Python 3.1.14 x64 on Win7
map()-based soluiton, appears to be the fastest one. Solution using itertools.starmap() and generator expression can also win on perfromance.The last two produce generators .In case of iterools.starmap() your lambda function should be changed a little. The 4th solution is list comprehensions.It appears to be the slowest. You can use code below to check performance on your platform.
import timeit
import itertools
repeat=10000
#generating 10000 element test list
big_list=[[x,y] for x,y in enumerate(range(10000))]
#setting configuration for timeit.timeit
conf_expr="from __main__ import big_list,my_f"
#giving name for lambda
my_f=lambda x:x+[sum(x)]
#tesing
print("map test")
print(timeit.timeit("map(my_f,big_list)",conf_expr,number=repeat))
print("itertool test")
#changing lambda for itertools.starmap() and giving name
my_sum=lambda x,y:[x,y]+[sum((x,y))]
#chanding configuration for timeit.timeit
conf_expr_i="from __main__ import my_sum,big_list"
print(timeit.timeit("itertools.starmap(my_sum,big_list)",conf_expr_i,number=repeat))
print("generator expr. test")
print(timeit.timeit("(i+[sum(i)] for i in big_list)",conf_expr,number=repeat))
print("list comprehensions test")
print(timeit.timeit("[i+[sum(i)] for i in big_list]",conf_expr,number=repeat))
Output
map test
0.00207904110842
itertool test
0.00228213246608
generator expr. test
0.0036744175886
list comprehensions test
52.2559879524
This might be interesting in optimization questions: http://www.python.org/doc/essays/list2str.html
Upvotes: 0
Reputation: 239483
You can use list comprehension, like this
my_list = [[1,1],[2,2],[3,3],[4,4]]
print [item + [sum(item)] for item in my_list]
Output
[[1, 1, 2], [2, 2, 4], [3, 3, 6], [4, 4, 8]]
If you want to fix your method, you can do it like this
print map(lambda x: x + [sum(x)], my_list)
List comprehension method would be faster than the map
method. So prefer that.
my_list = [[1,1],[2,2],[3,3],[4,4]]
from timeit import timeit
imports = "from __main__ import my_list"
print timeit("map(lambda x: x + [sum(x)], my_list)", imports)
print timeit("[item + [sum(item)] for item in my_list]", imports)
Output on my machine
1.58958506584 # map method
1.11319303513 # List comprehension method.
Upvotes: 2