Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pop/remove items out of a python tuple

Tags:

python

tuples

I am not sure if I can make myself clear but will try.

I have a tuple in python which I go through as follows (see code below). While going through it, I maintain a counter (let's call it 'n') and 'pop' items that meet a certain condition.

Now of course once I pop the first item, the numbering all goes wrong, how can I do what I want to do more elegantly while removing only certain entries of a tuple on the fly?

for x in tupleX:
  n=0
  if (condition):
     tupleX.pop(n)
  n=n+1
like image 808
Steve Grafton Avatar asked Feb 10 '14 16:02

Steve Grafton


People also ask

How do you pop things out of a tuple?

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 I remove something from a tuple in Python?

In Python, we can't delete an item from a tuple. Instead, we have to assign it to a new Tuple. In this example, we used tuple slicing and concatenation to remove the tuple item. The first one, numTuple[:3] + numTuple[4:] removes the third tuple item.

Does pop work on tuple?

tuple s are immutable, and don't have a pop method.

How do I remove something from a list in pop Python?

The remove() method removes the first matching element (which is passed as an argument) from the list. The pop() method removes an element at a given index, and will also return the removed item. You can also use the del keyword in Python to remove an element or slice from a list.


3 Answers

As DSM mentions, tuple's are immutable, but even for lists, a more elegant solution is to use filter:

tupleX = filter(str.isdigit, tupleX)

or, if condition is not a function, use a comprehension:

tupleX = [x for x in tupleX if x > 5]

if you really need tupleX to be a tuple, use a generator expression and pass that to tuple:

tupleX = tuple(x for x in tupleX if condition)
like image 110
SingleNegationElimination Avatar answered Oct 24 '22 10:10

SingleNegationElimination


Yes we can do it. First convert the tuple into an list, then delete the element in the list after that again convert back into tuple.

Demo:

my_tuple = (10, 20, 30, 40, 50)

# converting the tuple to the list
my_list = list(my_tuple)
print my_list  # output: [10, 20, 30, 40, 50]

# Here i wanna delete second element "20"
my_list.pop(1) # output: [10, 30, 40, 50]
# As you aware that pop(1) indicates second position

# Here i wanna remove the element "50"
my_list.remove(50) # output: [10, 30, 40]

# again converting the my_list back to my_tuple
my_tuple = tuple(my_list)


print my_tuple # output: (10, 30, 40)

Thanks

like image 24
Abdulvakaf K Avatar answered Oct 24 '22 09:10

Abdulvakaf K


In Python 3 this is no longer an issue, and you really don't want to use list comprehension, coercion, filters, functions or lambdas for something like this.

Just use

popped = unpopped[:-1]

Remember that it's an immutable, so you will have to reassign the value if you want it to change

my_tuple = my_tuple[:-1]

Example

>>> foo= 3,5,2,4,78,2,1
>>> foo
(3, 5, 2, 4, 78, 2, 1)
foo[:-1]
(3, 5, 2, 4, 78, 2)

If you want to have the popped value,,

>>> foo= 3,5,2,4,78,2,1
>>> foo
(3, 5, 2, 4, 78, 2, 1)
>>> foo, bit = foo[:-1], foo[-1]
>>> bit
1
>>> foo
(3, 5, 2, 4, 78, 2)

Or, to work with each value of a tuple starting at the back...

foo = 3,5,2,4,78,2,1
for f in reversed(foo):
    print(f)  # 1; 2; 78; ...

Or, with the count...

foo = 3,5,2,4,78,2,1
for f, i in enumerate(reversed(foo)):
    print(i, f)  # 0 1; 1 2; 2 78; ...

Or, to coerce into a list..

bar = [*foo]
#or 
bar = list(foo)
like image 6
Konchog Avatar answered Oct 24 '22 09:10

Konchog