I have a list of elements, Let's say something like below:
A = [1, 2, 3, 4, 5]
by using the enumerate
function, I'll get a list like below:
B = [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]
But I want to replace the indexes with a given list of numbers, for example: if I have a list like below:
C = [4, 6, 9, 10 , 22]
I want to change the B to a list like below:
B_modified = B = [(4, 1), (6, 2), (9, 3), (10, 4), (22, 5)]
How can I do that? Thanks
You should use the zip
function to combine these two lists.
A = [1, 2, 3, 4, 5]
C = [4, 6, 9, 10 , 22]
B = list(zip(C, A))
print(B)
Output:
[(4, 1), (6, 2), (9, 3), (10, 4), (22, 5)]
It can be used just like enumerate
:
for i, e in zip(C, A):
print(i, e)
Output:
4 1
6 2
9 3
10 4
22 5