-1

I have a list something like this:

l = [(1000, 'DONE'), (5, 'FAILED'), (1995, 'TO_DO')]

The (1995, 'TO_DO') moves to either 'DONE' or 'FAILED' one by one, This process takes some time (say about 5 minutes). I want to keep on checking for 'TO_DO' in list l and when it's done the script should be done.

So As soon as TO_DO gets disappear, the script should say - 'Process Complete' and need to wait until any occurrence of 'TO_DO' is there in the list.

Numbers can vary.

Ayush
  • 909
  • 1
  • 13
  • 32
  • I'm not entirely clear on what you want to do here. – Robert Moskal Aug 24 '15 at 14:18
  • can you be more clear , if you can add your work script that could be more helpful. – Sujay Narayanan Aug 24 '15 at 14:18
  • I sense a case of [XY](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) problem, here. You might want to get into more detail (e.g. who re-orders the list elements). It guess there is a better answer to your overall problem than checking for a specific list element. – dhke Aug 24 '15 at 14:22

3 Answers3

1

You can do

[ e for e in l if e[1] == 'TO_DO']

and check if the result is empty.

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
  • Or `if not next((x for x in l if x[1] == 'TO_DO'), None)` which avoids copying the list elements if there are many – dhke Aug 24 '15 at 14:21
0

You can use: if any('TO_DO' in elem for elem in l): .....

Got it from: Check if element exists in tuple of tuples

Community
  • 1
  • 1
equinox93
  • 133
  • 9
0

I am not 100% sure what you are trying to exactly achive but lets assume you have a script or function which you wish to carry out whenver 'TO_DO' is present within a tuple:

# If you are not using python 3
from time import sleep
from __future__ import print_function

l = [(1000, 'DONE'), (5, 'FAILED'), (1995, 'TO_DO')]
# Some example script
script = lambda (x, y): sleep(x) if y=='TO_DO' else print('process_complete')
map(script, l)

This does the following :

process_complete
process_complete
# waits for 1995 secs

Alternatively a list comprehension will also do:

# If you are not using python 3
from time import sleep
from __future__ import print_function
[sleep(x) if y == 'TO_DO' else print('process_complete') for x, y in l]

Hope this helps !!

Francisco Vargas
  • 653
  • 1
  • 7
  • 22