6

In python i have this code

if record[0][1]:

the problem is.. when mysql does not return anything and thus..

record[0][1]

has no data..

this python code fails:

if record[0][1]:
IndexError: tuple index out of range

i simply want it to move on to the "else" statement or simply consider this if statement as .. invalid given that

record[0][1]

has no value. or data.. ( incoming stuff from mysql )

1
  • 3
    You can always just try: ... except IndexError: ... Commented Aug 6, 2014 at 20:26

2 Answers 2

8
try:
    if record[0][1]:
        # Do stuff
except IndexError:
    pass
Sign up to request clarification or add additional context in comments.

2 Comments

That look like an anti-pattern as in Python you would just access the tuple - surrounded by a try/except - but actually not test if the data exist. Ask forgiveness not permission.
i am also getting an error like this stackoverflow.com/questions/46945860/…
2

You can use a try...except or use a short-circuiting check on outer tuple.

if len(record) > 0 and len(record[0]) > 1 and record[0][1]:

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.