Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีตรวจสอบว่า Key มีอยู่ใน Python Dictionary

ตรวจสอบ key ใน Python dictionary ด้วย operator in จับคู่ key กับ dictionary ตรวจสอบการมีอยู่ของ key

ตัวอย่าง

my_dict = {'name': 'TutorialsPoint', 'time': '15 years', 'location': 'India'}
print('name' in my_dict)
print('foo' in my_dict)

ผลลัพธ์

True
False