Python3 - How to Search Key by Values in a List of Dictionaries?
You can use a list comprehension:
data = [{'users': {'user_a': [{'email': ['aaa1@email.com', 'aaa2@email.com']}], 'user_b': [{'email': ['bbb1@email.com']}]}, 'class': 'class_A'}, {'users': {'user_d': [{'email': ['ddd1@email.com']}], 'user_c': [{'email': ['aaa1@email.com', 'ccc@email.com']}]}, 'class': 'class_B'}]
email = "aaa1@email.com"
result = [[i['class'], j] for i in data for j, k in i['users'].items() if any(email in x['email'] for x in k)]
Output:
[['class_A', 'user_a'], ['class_B', 'user_c']]
Iterate through a list of dictionaries to find most recent DateTime
You can sort the list in descending order based on the lastLogOnDateTime
field and just take the first dictionary of the list
res = []
s = sorted(list_of_dict, key=lambda d: d['lastLogOnDateTime'], reverse=True)
if len(s)>0:
res = [s[0]]
Python - Extract value from list of dictionaries that match a specified key
This can easily be done by looping on both the dictionary and the list, and then comparing the names:
def match_names(names, data):
ret_list = []
for name in names:
for val in data:
if val['name'] == name:
ret_list.append(name)
return ret_list
Find item in list of dicts
If all of the keys are unique, you can flatten the list of dictionaries into a dictionary with a straightforward dictionary comprehension.
Note: you don't want to use list
as a name for a variable, as it is an important built-in type. Use something like lst
instead.
{ k: v for d in lst for k, v in d.items() }
Result:
{'ENT_AUT': ['2018-11-27'], 'ENT_NAT_REF_COD': 'C87193',
'ENT_NAM': 'MONEYBASE LIMITED', 'ENT_NAM_COM': 'MONEYBASE LIMITED',
'ENT_ADD': 'Ewropa Business Centre, Triq Dun Karm',
'ENT_TOW_CIT_RES': 'Birkirkara', 'ENT_POS_COD': 'BKR 9034',
'ENT_COU_RES': 'MT'}
Getting the value for key 'ENT_NAM'
is now just:
{ k: v for d in lst for k, v in d.items() }['ENT_NAM']
find an item inside a list of dictionaries
As you found you have to iterate (unless you are able to change your data structure to an enclosing dict) why don't you just do it?
>>> [d['status'] for d in list_of_dicts if d['name']=='Robert']
[1]
Despite this, I recommend considering a map type (like dict) every time you see some 'id' field in a proposed data structure. If it's there you probably want to use it for general identification, instead of carrying dicts around. They can be used for relations also, and transfer easily into a relational database if you need it later.
Search by value of dict in array
Convert list
of dict
to dict
d = {x["token"]: x for x in large_list}
d["4kj13"]["value1"]
# 10
Related Topics
What Are the Advantages of Numpy Over Regular Python Lists
How to Fix "Runtimeerror: Package Fails to Pass a Sanity Check" For Numpy and Pandas
Finding Local Ip Addresses Using Python'S Stdlib
How to Play an Mp3 With Pygame
Installing Python Module Within Code
Remove All the Elements That Occur in One List from Another
How to Add a New Column to an Existing Dataframe
How to Call a Function Within a Class
Split String With Multiple Delimiters in Python
Are For-Loops in Pandas Really Bad? When Should I Care
How to Step Through Python Code to Help Debug Issues
How to Check If a String Represents an Int, Without Using Try/Except
Why Does This Iterative List-Growing Code Give Indexerror: List Assignment Index Out of Range
Sort a List of Tuples by 2Nd Item (Integer Value)