Python Forum

Full Version: (Fix it) Extend list with multiple other ones
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
We need to be able to automatically extend our list with multiple other ones, here's an example:
How it works now:
sales_w1 = [7,3,42,19,15,35,9] sales_w2 = [12,4,26,10,7,28] sales = [] sales.extend(sales_w1) sales.extend(sales_w2)
But could've been this:
sales_w1 = [7,3,42,19,15,35,9] sales_w2 = [12,4,26,10,7,28] sales = [] sales.extend(sales_w1, sales_w2)
How do I suggest the Python devs and contributors to get this function on board?
Thanks in advance!
You can do
from itertools import chain sales.extend(chain(sales_w1, sales_w2))
Alternatively just unpack:

sales = [*sales_w1, *sales_w2]
Wouldn't it be better to keep the weeks apart, so you can directly refer to any given week?

sales = {} for i in range(1, 53): # assume the poor staff must work 7 days a week, 52 weeks a year! sales[f'sales_w{str(i)}'] = [randint(1, 50) for i in range(7)] 
How did we do in sales week 25?

print(sales['sales_w25'])
Output:
[39, 10, 7, 47, 17, 38, 12]
But if you really want all the sales together:

total_sales = [] for value in sales.values(): total_sales = total_sales + value