How can I check if a list index exists?

后端 未结 4 934
刺人心
刺人心 2021-01-17 07:59

Seems as though

if not mylist[1]:
    return False

Doesn\'t work.

4条回答
  •  南方客
    南方客 (楼主)
    2021-01-17 08:26

    In the EAFP style of Python:

    try:
        mylist[1]
    except IndexError:
        print "Index doesn't exist!"
    

提交回复
热议问题