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?
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)]
list1 = [1]
list2 = [2]
list3 = [3]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With