Azfar Faizan - 2 months ago 13

Python Question

I have a list of tuples

`a = [('BC', 'CONTAINS'), ('CD', 'FREE_FROM'), ('BM', 'CONTAINS') , ('ZZ', nan), (nan, nan), (nan, 'FREE_FROM'), (nan, nan)]`

I want a list which does not contain any nan and also does not contain element in which second element of tuple is 'FREE_FROM' such that the resulting list looks like the following:

`res = [('BC', 'CONTAINS'), ('BM', 'CONTAINS')]`

Currently I am doing it like this:

`res = [(x,y) for x,y in containsAllergen if (str(x,y) != ('nan', 'nan') or str(y) != 'FREE_FROM')]`

but it is throwing the error mentioned in the subject:

`TypeError: decoding str is not supported`

Any suggestions? Thanks.

Answer

I think the error is in :

```
str(x,y)
```

Beside, I think you will need the "and" operator to produce the result you need:

```
res = [(x,y) for x,y in a if (str(x) != 'nan' and str(y) != 'nan' and str(y) != 'FREE_FROM')]
```