Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a way to catch this unexpected value

[(9, '['), (25, ']'), (442, '['), (460, ']'), (558, '['), (576, ']'), (608, ']')]

In this list of tuples I am trying to find key of "]" in the end.
If you take a closer look values goes like this;

[ => ] => [ => ] => [ => ] => [ => ]

But at last step it goes;

] => ]

I want to find key when this unexpected value appear.
How can I do that?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I think you’re looking for something like this:

from itertools import cycle

L = [(9, '['), (25, ']'), (442, '['), (460, ']'), (558, '['), (576, ']'), (608, ']')]
c = cycle(['[', ']'])

for t in L:
    if t[1] != next(c):
        print(t[0])
        break
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading