I have a list of tuples as follows:
values = [('1', 'hi', 'you'), ('2',' bye', 'bye')]
However, the first element of each tuple is not needed. The desired output is:
[('hi', 'you'), (' bye', 'bye')]
I've done enough research to know I can't manipulate tuples but I can't seem to find an answer on how to successfully remove the first element of each tuple in the list.
By definition, tuple object is immutable. Hence it is not possible to remove element from it. However, a workaround would be convert tuple to a list, remove desired element from list and convert it back to a tuple.
Use slice to remove the first element To remove the first element from a tuple in Python, slice the original tuple from the second element (element at index 1) to the end of the tuple. This will result in a new tuple with the first element removed.
The remove() function allows you to remove the first instance of a specified value from the list. This can be used to remove the list's top item. Pick the first member from the list and feed it to the remove() function.
Firstly tuple is immutable.
Secondly try this approach using a list comprehension:
a_list = [el[1:] for el in values]
Check slice notation
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With