I need to write a function that takes a string \'(1,2,3,4,5),(5,4,3,2,1)\' and returns a list of tuples of the 1st and last element of each tuple, [(1,5),(5,1)]. I was think
In this case, the ast module could be useful:
>>> from ast import literal_eval
>>> s = '(1,2,3,4,5),(5,4,3,2,1)'
>>> my_tuples = literal_eval(s)
>>> my_tuples
((1, 2, 3, 4, 5), (5, 4, 3, 2, 1))
So, my_tuples has a tuple with the tuples of your string. Now, we can get the first and last element of all your tuples using a list comprehension:
>> new_tuples = [(t[0], t[-1]) for t in my_tuples]
>>> new_tuples
[(1, 5), (5, 1)]