Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you append to multiple lists at once in python?

Say I have the list:

mylist=[9, 8, 7, 6, 5, 6 , 7, 8, 9]

and 9 empty lists:

list1=[]
list2=[]
list3=[]

and so on.. Is there a way of using a for loop to append mylist[0] to list1, mylist[1] to list2 etc. for all 9 items, or do I have to code for each of them separately?

like image 208
Oceanescence Avatar asked Oct 27 '25 22:10

Oceanescence


1 Answers

I believe this is a fairly simple way of appending to multiple lists at once.

list1, list2, list3 = [], [], []
my_list = [1,2,3]

[x.append(y) for x,y in zip([list1, list2, list3], my_list)]

Output

list1 = [1]

list2 = [2]

list3 = [3]

like image 165
Ali Avatar answered Oct 29 '25 13:10

Ali



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!