How can I check if a list index exists?

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

Seems as though

if not mylist[1]:
    return False

Doesn\'t work.

4条回答
  •  执念已碎
    2021-01-17 08:09

    In the case of integer-indexed lists, I'd simply do

    if 1 < len(mylist):
      ...
    

    For dicts, you can of course do

    if key in mydict:
      ...
    

提交回复
热议问题