7

I am trying to remove all strings from a list of tuples

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

I have started to try and find a solution:

output = [i for i in ListTuples if i[0] == str]

print(output)

But I can't seem to get my head around how I would get an output like:

[(100), (80), (20), (40), (40)]

The format is always (int, str).

3
  • 1
    Do you know that you are always going to have items of the form [(<int>, <str>), ...]? Commented Jan 16, 2019 at 14:06
  • 2
    If the format is always (int, str) the following could be a simple solution: output = [(i[0],) for i in ListTuples] Commented Jan 16, 2019 at 14:09
  • @MarkNijboer Yes this is also perfect Commented Jan 16, 2019 at 14:12

5 Answers 5

9

Use a nested tuple comprehension and isinstance:

output = [tuple(j for j in i if not isinstance(j, str)) for i in ListTuples]

Output:

[(100,), (80,), (20,), (40,), (40,)]

Note that there are trailing commas in the tuples to distinguish them from e.g. (100) which is identical to 100.

Sign up to request clarification or add additional context in comments.

7 Comments

This answer does no match OP's desired output!
@RoadRunner, Not true, [(100), (80), (20), (40), (40)] is valid Python, it's a list of integers!
Thank you, this worked perfectly! I just have to change the isinstance(j, str) to isinstance(j, int) to get the integer values I'm after. I will accept this answer
I think this does match OP's desired output, or rather what OP (probably) intended to show, not how that's interpreted by Python.
@a.rowland If you want to "remove all strings", then you should use not isinstance(j, str), otherwise it would be "keep all integers", which is not strictly the same. (In your example, both would be equivalent, though.)
|
2

Since extracting the first item of each tuple is sufficient, you can unpack and use a list comprehension. For a list of tuples:

res = [(value,) for value, _ in ListTuples]  # [(100,), (80,), (20,), (40,), (40,)]

If you need just a list of integers:

res = [value for value, _ in ListTuples]     # [100, 80, 20, 40, 40]

For a functional alternative to the latter, you can use operator.itemgetter:

from operator import itemgetter
res = list(map(itemgetter(0), ListTuples))   # [100, 80, 20, 40, 40]

Comments

1

Here's another solution using filter():

def non_string(x):
    return not isinstance(x, str)

print([tuple(filter(non_string, x)) for x in ListTuples])
# [(100,), (80,), (20,), (40,), (40,)]

Comments

0

Try this

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

ListInt = []
ListStr = []

for item in ListTuples:
    strAux = ''
    intAux = ''
    for i in range(0, len(item)):

        if(isinstance(item[i], str)):
            strAux+=item[i] 
        else:
            intAux+=str(item[i])


    ListStr.append(strAux)
    ListInt.append(intAux)


print(ListStr)

'''TO CONVERT THE STR LIST TO AN INT LIST'''
output= list(map(int, ListInt)) 
print(output)

The output is [100, 80, 20, 40, 40]

3 Comments

What are those "aux" accumulators for? Why not directly appen in the if/else? This will fail for [(1, "foo", 3)] or any other tuple that has more than one of each type, and for any other cases those "aux" variables have no use, either.
If you appen in if/else it you retrieve 'A', 'A', 'A', 'B' 'B' and so on, because its inside for loop. If you try [(1, "foo", 3)]it will return 13 and foo
No, it does not, as you are not iterating the characters in the string, just the elements in the tuple. And, yes, it will return that, and I don't think that that's what OP intended.
0

We check if a particular value is a string or not using type(value).

output = [tuple([j for j in i if type(j)!=str]) for i in ListTuples]
print(output)
    [(100,), (80,), (20,), (40,), (40,)]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.