Answers for "search in array of dictionary in python"

1

python find dict in list of dict by id

>>> dicts = [
     { "name": "Tom", "age": 10 },
     { "name": "Mark", "age": 5 },
     { "name": "Pam", "age": 7 },
     { "name": "Dick", "age": 12 }
 ]
>>> next(item for item in dicts if item["name"] == "Pam")
{'age': 7, 'name': 'Pam'}

# WITH DEFAULT VALUE TO None if None
next((item for item in dicts if item["name"] == "pam"), None)
Posted by: Guest on March-10-2021
2

find a key in a dictionary python

# How to find a key / check if a key is in a dict

# Easiest way
fruits = {'apple': 2, 'pear': 5, 'strawberry': 3}

if 'apple' in fruits:
	print("Key found!")
else:
  	print("Key not found!")
Posted by: Guest on January-21-2021

Code answers related to "search in array of dictionary in python"

Python Answers by Framework

Browse Popular Code Answers by Language