I am using tweepy to retrieve links in a json.I have extracted a list with dictionaries inside. Most of them are in the same pattern (same order for the keys and values) like below
[{'bitrate': 2176000, 'content_type': 'video/mp4', 'url': 'https://A_LINK'},
{'bitrate': 632000, 'content_type': 'video/mp4', 'url': 'https://B_LINK'},
{'content_type': 'application/x-mpegURL', 'url': 'https://C_LINK'},
{'bitrate': 832000, 'content_type': 'video/mp4', 'url': 'https://D_LINK'}]
There are two characteristics of above pattern:
1.{'bitrate': 2176000, 'content_type': 'video/mp4', 'url': 'https://A_LINK'}
is at the FRONT of {'content_type': 'application/x-mpegURL', 'url': 'https://C_LINK'}
2.{'content_type': 'application/x-mpegURL', 'url': 'https://C_LINK'}
is MISSING the key Bitrate
I want to find the url which has the bitrate of 2176000 (Which is https://A_LINK for the example),the below codes are working with above patterns which I can find https://A_LINK
link = next((item for item in x if item["bitrate"] == 2176000), None)
print(link["url"])
However, as {'content_type': 'application/x-mpegURL', 'url': 'https://C_LINK'}
is MISSING the key Bitrate
It will cause errors in the below patterns.
[{'bitrate': 632000, 'content_type': 'video/mp4', 'url': 'https://A_LINK'},
{'bitrate': 832000, 'content_type': 'video/mp4', 'url': 'https://B_LINK'},
{'content_type': 'application/x-mpegURL', 'url': 'https://C_LINK'},
{'bitrate': 2176000, 'content_type': 'video/mp4', 'url': 'https://D_LINK'}]
For the above pattern,
{'bitrate': 2176000, 'content_type': 'video/mp4', 'url': 'https://D_LINK'}
is located AFTER 'content_type': 'application/x-mpegURL'
Whenever I receive pattern like this, I receive error
KeyError: 'bitrate'
and I can't get the https://D_LINK AFTER {'content_type': 'application/x-mpegURL', 'url': 'https://C_LINK'}
Therefore I am asking, when I receive the 2nd list pattern,
is there a way to so-called "bypass" {'content_type': 'application/x-mpegURL', 'url': 'https://C_LINK'}
so that I can get the https://D_LINK at last which has the bitrate 2176000?
Thank you.
Is this what you're looking for?
l = [
{'bitrate': 632000, 'content_type': 'video/mp4', 'url': 'https://A_LINK'},
{'bitrate': 832000, 'content_type': 'video/mp4', 'url': 'https://B_LINK'},
{'content_type': 'application/x-mpegURL', 'url': 'https://C_LINK'},
{'bitrate': 2176000, 'content_type': 'video/mp4', 'url': 'https://D_LINK'},
]
''.join([d["url"] for d in l if "bitrate" in d.keys() and d['bitrate'] == 2176000])
Output:
'https://D_LINK'