Say I have several lists
A = [1,2,3]
B = [4,5,6]
C = [7,8,9]
How can I create new lists so that they contain matching indexes, as in:
D = [1,4,7]
E = [2,5,8]
F = [3,6,9]
The original lists will always contain the same number of elements, and I need this to work for any number of elements and any number of lists, not just three. I figure I need to loop over a range, but I’m not sure how to go about it.
If I understand you correctly, you may be looking for zip():
If instead of several lists, you have a list of the lists that you want to zip, then you can use
*for unpacking the sublists, as follows: