Python 如何知道列表索引是否存在

Python 如何知道列表索引是否存在,python,Python,我有一个python列表,里面有dicts: 'hotelesDisponibles': [{ 'hotelCode': 'ROL01', 'nombre': 'ROL01', 'categoria': '4E', 'descripcion': 'Hotel de prueba con 3 habitaciones y 3 regímenes 2 grupos de tarifa: -General con extra -No reembolsable co

我有一个python列表,里面有dicts:

'hotelesDisponibles': [{
    'hotelCode': 'ROL01', 
    'nombre': 'ROL01', 
    'categoria': '4E', 
    'descripcion': 'Hotel de prueba con 3 habitaciones y 3 regímenes 2 grupos de tarifa: -General con extra -No reembolsable con descuento y gastos de cancelación 1 promoción con 5% descuento', 
    'habitacion': { 
        'RoomRQ': '1', 
        'precioSinDesc': '1171.8'
    } 
},{
    'hotelCode': 'ROL03', 
    'nombre': 'ROL03',
    'categoria': '4E', 
    'descripcion': 'Hotel con 3 habitaciones y 4 regímenes 3 grupos de tarifa: -General -No reembolsable con descuento y gastos de cancelación -Tarifa con extras', 
},{ 
    'hotelCode': 'ROL02',
    'nombre': 'ROL02',
    'categoria': '4E', 
    'descripcion': 'Hotel con 4 habitaciones y 4 regímenes 1 grupo de tarifa: General 1 promoción sobre tarifa General no reembolsable (descuento + gasto de cancelación)', 
    'habitacion': { 
        'RoomRQ': '1', 
        'precioSinDesc': '1320.0'
    } 
}]
有些词典可能有一个列表
habitacion{}
在里面,也可能没有。我怎么知道这条格言是否存在

[d for d in list_of_dicts if 'habitaction' in d]

将返回一个列表,其中包含具有
'habitation'
键以及
中的
,可能

for index, innerdict in enumerate(yourdata['hotelesDisponibles']):
    print("Index:", index, "Habitacion:", "Yes" if 'habitacion' in innerdict else "No")

您可以使用“in”检查任何密钥,例如:

for hdic in d['hotelesDisponibles']:
    if 'habitacion' in hdic:
        print 'Habitacion Found'

你读过关于词典的文档吗?