for i,x in enumerate(['A','B','C']):
print(i, x)
Current output is:
0 A
1 B
2 C
I need the output to be as [(0,A), (1,B), (2,C)]
CodePudding user response:
Just convert the enumerate
iterator to a list:
out = list(enumerate(['A','B','C']))
Output:[(0,A), (1,B), (2,C)]
(the output is a list object)
If you just want to print:
print(list(enumerate(['A','B','C'])))
Finally, if you're looking to build a string with your specific format, use:
print('[%s]' % ','.join(f'({i},{v})' for i,v in enumerate(['A','B','C'])))
Output: '[(0,A),(1,B),(2,C)]'
CodePudding user response:
Wrap your call to enumerate
in a call to list
:
out = list(enumerate(['A', 'B', 'C']))
Output:
>>> out
[(0, 'A'), (1, 'B'), (2, 'C')]