I
I
Ivan Yakushenko2019-04-27 11:17:23
Python
Ivan Yakushenko, 2019-04-27 11:17:23

How to refer to a dictionary element in a list?

VK API returns the following string when accessing event:

spoiler
<<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})>

I can also access event.object.attachments[0] and get the following result:
spoiler
{'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'}}

How do I access the 'sizes' element? For example, I want to check if it exists:
if event.object.attachments[0].get('photo').get('sizes'):
    print(event.object.attachments[0].get('photo').get('sizes'))

If it exists, then I will get the correct text:
spoiler
[{'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}]

But if 'attachments': [], then I get the following error:
IndexError: list index out of range

Code option:
if 'sizes' in event.object.attachments[0].get('photo'):
    print(event.object.attachments[0].get('photo').get('sizes'))

It behaves exactly the same.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alfss, 2019-04-27
@kshnkvn

If event.object.attachments
Add

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question