Answer the question
In order to leave comments, you need to log in
How to refer to a dictionary element in a list?
VK API returns the following string when accessing event:
<<class 'vk_api.bot_longpoll.VkBotMessageEvent'>({'type': 'message_new', 'object': {'date': 1556352030, 'from_id': 463080516, 'id': 177, 'out': 0, 'peer_id': 463080516, 'text': '', 'conversation_message_id': 160, 'fwd_messages': [], 'important': False, 'random_id': 0, 'attachments': [{'type': 'photo', 'photo': {'id': 456239052, 'album_id': -3, 'owner_id': 463080516, 'sizes': [{'type': 's', 'url': 'https://pp.userapi.com/c846418/v846418123/1f7149/62si_sZGRZk.jpg', 'width': 75, 'height': 63}, {'type': 'm', 'url': 'https://pp.userapi.com/c846418/v846418123/1f714a/3Ftc3hduXEY.jpg', 'width': 130, 'height': 110}, {'type': 'x', 'url': 'https://pp.userapi.com/c846418/v846418123/1f714b/6IC1XLC1Ob4.jpg', 'width': 406, 'height': 342}, {'type': 'o', 'url': 'https://pp.userapi.com/c846418/v846418123/1f714c/s-SjcQb39pk.jpg', 'width': 130, 'height': 110}, {'type': 'p', 'url': 'https://pp.userapi.com/c846418/v846418123/1f714d/oWX8vWaZ3aU.jpg', 'width': 200, 'height': 168}, {'type': 'q', 'url': 'https://pp.userapi.com/c846418/v846418123/1f714e/BAZGquBKmmc.jpg', 'width': 320, 'height': 270}, {'type': 'r', 'url': 'https://pp.userapi.com/c846418/v846418123/1f714f/IsFBl5eTwvA.jpg', 'width': 406, 'height': 342}], 'text': '', 'date': 1556352029, 'access_key': '5bb46401e1d3bc96ca'}}], 'is_hidden': False}, 'group_id': 38269868})>
{'type': 'photo', 'photo': {'id': 456239054, 'album_id': -3, 'owner_id': 463080516, 'sizes': [{'type': 's', 'url': 'https://pp.userapi.com/c846418/v846418123/1f7157/aNhs5zduBBQ.jpg', 'width': 75, 'height': 75}, {'type': 'm', 'url': 'https://pp.userapi.com/c846418/v846418123/1f7158/D2E9DolFpD4.jpg', 'width': 130, 'height': 130}, {'type': 'x', 'url': 'https://pp.userapi.com/c846418/v846418123/1f7159/CLOrtHFUkeE.jpg', 'width': 230, 'height': 230}, {'type': 'o', 'url': 'https://pp.userapi.com/c846418/v846418123/1f715a/pzljfevz7H4.jpg', 'width': 130, 'height': 130}, {'type': 'p', 'url': 'https://pp.userapi.com/c846418/v846418123/1f715b/NN1BsEJejmY.jpg', 'width': 200, 'height': 200}, {'type': 'q', 'url': 'https://pp.userapi.com/c846418/v846418123/1f715c/234E--asbQk.jpg', 'width': 230, 'height': 230}, {'type': 'r', 'url': 'https://pp.userapi.com/c846418/v846418123/1f715d/7_ds-neilPU.jpg', 'width': 230, 'height': 230}], 'text': '', 'date': 1556352221, 'access_key': 'cab850e59fd2dbdaa2'}}
if event.object.attachments[0].get('photo').get('sizes'):
print(event.object.attachments[0].get('photo').get('sizes'))
[{'type': 's', 'url': 'https://pp.userapi.com/c846418/v846418123/1f71b2/nLGZPgEkVs4.jpg', 'width': 75, 'height': 75}, {'type': 'm', 'url': 'https://pp.userapi.com/c846418/v846418123/1f71b3/UUN1y358yKU.jpg', 'width': 100, 'height': 100}, {'type': 'x', 'url': 'https://pp.userapi.com/c846418/v846418123/1f71b4/0a2fqfU4U2k.jpg', 'width': 100, 'height': 100}, {'type': 'o', 'url': 'https://pp.userapi.com/c846418/v846418123/1f71b5/pu87I7W8g4A.jpg', 'width': 100, 'height': 100}, {'type': 'p', 'url': 'https://pp.userapi.com/c846418/v846418123/1f71b6/VZQ5vcP9Tdc.jpg', 'width': 100, 'height': 100}, {'type': 'q', 'url': 'https://pp.userapi.com/c846418/v846418123/1f71b7/JvWvFYkWVA8.jpg', 'width': 100, 'height': 100}, {'type': 'r', 'url': 'https://pp.userapi.com/c846418/v846418123/1f71b8/HBFUPEdXPIc.jpg', 'width': 100, 'height': 100}]
IndexError: list index out of range
if 'sizes' in event.object.attachments[0].get('photo'):
print(event.object.attachments[0].get('photo').get('sizes'))
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question