user3880134
user3880134

Reputation: 63

Python : IndexError: tuple index out of range

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 )

Upvotes: 6

Views: 19524

Answers (2)

celeritas
celeritas

Reputation: 2281

try:
    if record[0][1]:
        # Do stuff
except IndexError:
    pass

Upvotes: 8

merlin2011
merlin2011

Reputation: 75545

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]:

Upvotes: 2

Related Questions