Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if python dictionary contains value and if so return related value

I have a list of dicts, which contain filenames and modification dates in the following format:

fileList = [{"fileName": "file1.txt", "fileMod": "0000048723"}, 
            {"fileName": "file2.txt", "fileMod": "0000098573"}]

I need to query if a fileName exists in the dictionary and if so return the fileMod value for that entry.

like image 379
Finglish Avatar asked Dec 05 '22 18:12

Finglish


1 Answers

Using a list comprehension:

fileMod = [item['fileMod'] for item in fileList if item['fileName'] == filename]
like image 55
Pablo Avatar answered May 23 '23 21:05

Pablo