Assign dict values to list if they exist, otherwise none

Assign dict values to list if they exist, otherwise none



Right now, i have something like that:


media_info = MediaInfo.parse(file_path).to_data()
local_file_info = [ media_info['tracks'][1]['frame_rate'],
media_info['tracks'][1]['other_display_aspect_ratio'],
media_info['tracks'][2]['other_sampling_rate'],
media_info['tracks'][2]['other_channel_s'],
media_info['tracks'][3]['Format']]



How can i dumbproof this to assign None instead of throwing IndexError when element doesn't exist? (Example: file parsed with MediaInfo doesn't have subtitle track in video file, so there is no 3rd track)



I know i could do it the dirty way:


try:
local_file_info.append(media_info['tracks'][1]['frame_rate'])
except IndexError
local_file_info.append(None)

try:
local_file_info.append(media_info['tracks'][1]['other_display_aspect_ratio'])
except IndexError
local_file_info.append(None)

try:
local_file_info.append(media_info['tracks'][2]['other_sampling_rate'])
except IndexError
local_file_info.append(None)

try:
local_file_info.append(media_info['tracks'][2]['other_channel_s'])
except IndexError
local_file_info.append(None)

try:
local_file_info.append(media_info['tracks'][3]['Format'])
except IndexError
local_file_info.append(None)



But that just looks horrible and i'm sure there is better way to do that.





Why not just do it with for-loop?
– Lev Zakharov
Sep 3 at 10:50




2 Answers
2



The obvious problem with your code is the massive code duplication. Two useful tools for reducing code duplication are


media_info


try...except



Ideally, you should do both:


media_keys = [
(1, 'frame_rate'),
(1, 'other_display_aspect_ratio'),
(2, 'other_samping_rate'),
(2, 'other_channel_s'),
(3, 'Format')
]

def get_media_info(index, key):
try:
return media_info['tracks'][index][key]
except IndexError:
return None

local_file_info = [get_media_info(*keys) for keys in media_keys]





I like your implementation better :). List comprehensions are the way to go.
– jpp
Sep 3 at 11:14



a quick fix would be using if conditions


if 3 in media_info['tracks'].keys():
local_file_info.append(media_info['tracks'][3]['Format'])
else:
local_file_info.append(None)



but it still doesn't fix your problem, since it still looks horrible but atleast its not the dirty way





"Not the dirty way"? This is far dirtier than the OP's solution. You can't make code better by making it less DRY.
– Aran-Fey
Sep 3 at 10:51




Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế