Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing first elements of tuples in a list

Tags:

python

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.

like image 263
user47467 Avatar asked Oct 02 '15 14:10

user47467


People also ask

How do I remove the first tuple from a 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.

How do you remove the first element of a tuple in a list in Python?

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.

How do I remove the first occurrence from a list?

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.


1 Answers

Firstly tuple is immutable.

Secondly try this approach using a list comprehension:

a_list = [el[1:] for el in values]

Check slice notation.

like image 200
xiº Avatar answered Sep 19 '22 06:09

xiº