Remove tuple from list of tuples if certain condition is met
Asked Answered
T

6

19

I have a list of tuples that look like this;

ListTuples = [(100, 'AAA'), (80, 'BBB'), (20, 'CCC'), (40, 'DDD')]

I want to remove the tuples when the first element of the tuple is less than 50. The OutputList will look like this;

OutputList = [(100, 'AAA'), (80, 'BBB')]

How can this be done in python?

Thank you very much for your help.

Tobit answered 9/4, 2014 at 9:0 Comment(0)
G
45

You can easily do it as:

out_tup = [i for i in in_tup if i[0] >= 50]

[Out]: [(100, 'AAA'), (80, 'BBB')]

This simply creates a new list of tuples with only those tuples whose first element is greater than or equal to 50. Same result, however the approach is different. Instead of removing invalid tuples you accept the valid ones.

Grosmark answered 9/4, 2014 at 9:1 Comment(0)
C
7

You can also do:

>>> OutputList = filter(ListTuples, lambda x: x[0] >= 50)
>>> OutputList
[(100, 'AAA'), (80, 'BBB')]
Chengtu answered 9/4, 2014 at 9:12 Comment(7)
May I ask whether using filter and lambda be slower than the answer provided by sshashank124?Tobit
@user3293156 generally filter is slightly faster as it is a builtin function.Chengtu
@AlexThornton Only when used with built-in functions, with lambdas it is slow.Safier
@Aशwiniचhaudhary I suppose, but the difference should be marginal.Chengtu
Wow! But due to my incompetence in python, this code using filter and lambda is less readable compared to sshashank124's. I have to study what these 2 functions are about first.Tobit
@ Aशwini चhaudhary: Do you mean code with lambdas are slower?Tobit
Note that this raises an error. The syntax needs to be filter(function, list), so OutputList = filter(lambda x: x[0] >= 50, ListTuples )Illaffected
S
4

Code snippet for timing the solutions given by sshashank124 and Alex Thornton:

ListTuples = [(100, 'AAA'), (80, 'BBB'), (20, 'CCC'), (40, 'DDD')]
import timeit
timeit.timeit('[i for i in ListTuples if i[0] >= 50]', globals=globals(), number=1000000)
>>> 0.6041104920150246

timeit.timeit('filter(lambda x: x[0] >= 50, ListTuples)', globals=globals(), number=1000000)
>>> 0.3009479799948167

The build-in filter() solution is faster for this example.

Sunstone answered 13/7, 2017 at 11:28 Comment(0)
G
2

We can Do this with simple counter and a new list:

   new = []

   for i in ListTuples:
        for j in i:
            if ( counter % 2 == 0 ):
                if ( j > 50):
                    new.append(i)
            counter = counter +1

output :

[(100, 'AAA') , (80, 'BBB')]
Godolphin answered 29/1, 2018 at 14:0 Comment(0)
C
1

Try this,

>>> ListTuples = [(100, 'AAA'), (80, 'BBB'), (20, 'CCC'), (40, 'DDD')]
>>> new=[]
>>> for i in ListTuples:
    if i[0]>50:
        new.append(i)


>>> new
[(100, 'AAA'), (80, 'BBB')]
>>>
Cigar answered 9/4, 2014 at 9:14 Comment(1)
This code looks familiar. It looks like the C language kind of syntax. But is the execution slower than the pythonic code provided by Alex Thornton and sshashank124?Tobit
H
1
ListTuples = [(100, 'AAA'), (80, 'BBB'), (20, 'CCC'), (40, 'DDD')]
out=[]
for item in ListTuples:
    if item[0]>50:
        out.append((item[0],item[1]))
print(out)
Hofuf answered 30/8, 2021 at 15:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.