How to reverse each tuple in a list of tuples with Python

2 Answers

0 votes
def reverse_tuples(lst_tpl): 
    return [t[::-1] for t in lst_tpl] 
              
lst_tpl = [(1, 2, 3), (4, 5, 6, 7), (8, 9)] 

print(reverse_tuples(lst_tpl)) 



'''
run:

[(3, 2, 1), (7, 6, 5, 4), (9, 8)]

'''

 



answered Dec 17, 2019 by avibootz
0 votes
def reverse_tuples(lst_tpl): 
    return [tuple(reversed(t)) for t in lst_tpl] 
              
lst_tpl = [(1, 2, 3), (4, 5, 6, 7), (8, 9)] 

print(reverse_tuples(lst_tpl)) 



'''
run:

[(3, 2, 1), (7, 6, 5, 4), (9, 8)]

'''

 



answered Dec 17, 2019 by avibootz
...