How to check if the n-th element exists in a Python list?

后端 未结 2 916
南笙
南笙 2021-02-07 21:03

I have a list in python

x = [\'a\',\'b\',\'c\']

with 3 elements. I want to check if a 4th element exists without receiving an error message.

2条回答
  •  旧巷少年郎
    2021-02-07 21:37

    You want to check if the list is 4 or more elements long?

    len(x) >= 4
    

    You want to check if what would be the fourth element in a series is in a list?

    'd' in x
    

提交回复
热议问题