pop/remove items out of a python tuple

Question:

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
Asked By: Steve Grafton

||

Answers:

Maybe you want dictionaries?

d = dict( (i,value) for i,value in enumerate(tple))
while d:
    bla bla bla
    del b[x]
Answered By: tk.

ok I figured out a crude way of doing it.

I store the “n” value in the for loop when condition is satisfied in a list (lets call it delList) then do the following:

    for ii in sorted(delList, reverse=True):
    tupleX.pop(ii)

Any other suggestions are welcome too.

Answered By: Steve Grafton

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)

There is a simple but practical solution.

As DSM said, tuples are immutable, but we know Lists are mutable.
So if you change a tuple to a list, it will be mutable. Then you can delete the items by the condition, then after changing the type to a tuple again. That’s it.

Please look at the codes below:

tuplex = list(tuplex)
for x in tuplex:
  if (condition):
     tuplex.pop(tuplex.index(x))
tuplex = tuple(tuplex)
print(tuplex)

For example, the following procedure will delete all even numbers from a given tuple.

tuplex = (1, 2, 3, 4, 5, 6, 7, 8, 9)
tuplex = list(tuplex)
for x in tuplex:
  if (x % 2 == 0):
     tuplex.pop(tuplex.index(x))
tuplex = tuple(tuplex)
print(tuplex)

if you test the type of the last tuplex, you will find it is a tuple.

Finally, if you want to define an index counter as you did (i.e., n), you should initialize it before the loop, not in the loop.

Answered By: Reza K Ghazi

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

Answered By: Abdulvakaf K

The best solution is the tuple applied to a list comprehension, but to extract one
item this could work:

def pop_tuple(tuple, n):
    return tuple[:n]+tuple[n+1:], tuple[n]
Answered By: ShadowPuppy

say you have a dict with tuples as keys, e.g: labels = {(1,2,0): 'label_1'} you can modify the elements of the tuple keys as follows:

formatted_labels = {(elem[0],elem[1]):labels[elem] for elem in labels}

Here, we ignore the last elements.

Answered By: Chidi

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)
Answered By: Konchog

One solution is to convert to set and bring back to tuple

tupleX = (
    "ZAR",
    "PAL",
    "SEV",
    "ALC",
    "LPA",
    "TFN",)

remove = (
    "LPA",
    "TFN",)

tuple(set(tupleX) - set(remove))

(‘ZAR’, ‘PAL’, ‘ALC’, ‘SEV’)

A shorter way perhaps:

tup = (0, 1, 2, 3)
new_tup = (*tup[:-2], tup[-1])
print(new_tup) # (0, 1, 3)
Answered By: cisimon7
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.