Python Read List of Dictionaries From File
Search a List of Dictionaries in Python
- Use the
next()
Office to Search a List of Dictionaries in Python - Search a Listing of Dictionaries With the
filter()
Function in Python - Use List Comprehension to Search a List of Dictionaries in Python
This tutorial will introduce the methods you can use to search a listing of dictionaries in Python.
Use the next()
Function to Search a List of Dictionaries in Python
The next()
function can be utilized to provide the result as the adjacent item in the given iterator. This method besides requires the apply of the for
loop to test the process against all the conditions.
The following code uses the side by side()
office to search a listing of dictionaries in Python.
lstdict = [ { "proper name": "Klaus", "age": 32 }, { "name": "Elijah", "age": 33 }, { "proper name": "Kol", "age": 28 }, { "name": "Stefan", "age": 8 } ] print(side by side(ten for ten in lstdict if 10["name"] == "Klaus")) print(next(x for x in lstdict if x["proper name"] == "David"))
Output:
{'name': 'Klaus', 'historic period': 32} Traceback (about recent call terminal): File "<string>", line viii, in <module> StopIteration
This method is successfully implemented when we search for a name that already exists in the list of dictionaries. Yet, it gives a StopIteration
error when a name that doesn't exist in the list of dictionaries is searched.
However, this problem can exist easily treated in the lawmaking in a higher place. You lot simply tweak and provide a default with the use of a slightly dissimilar API.
lstdict = [ { "name": "Klaus", "age": 32 }, { "name": "Elijah", "historic period": 33 }, { "name": "Kol", "age": 28 }, { "name": "Stefan", "age": eight } ] print(side by side((x for x in lstdict if x["name"] == "David"), None))
Output:
None
Rather than finding the item itself, we can also observe the item's index in a List of Dictionaries. To implement this, we can use the enumerate()
function.
The post-obit code uses the next()
part and the enumerate()
role to search and find the item's alphabetize.
lstdict = [ { "proper noun": "Klaus", "age": 32 }, { "proper noun": "Elijah", "historic period": 33 }, { "proper noun": "Kol", "historic period": 28 }, { "proper noun": "Stefan", "age": 8 } ] print(adjacent((i for i, x in enumerate(lstdict) if x["proper name"] == "Kol"), None))
Output:
ii
Search a List of Dictionaries With the filter()
Function in Python
The filter(role, sequence)
function is used to compare the sequence
with the function
in Python. It checks each chemical element in the sequence to exist truthful or not according to the role. We can easily search a list of dictionaries for an item past using the filter()
part with a lambda
function. In Python3, the filter()
function returns an object of the filter
class. We tin can convert that object into a list with the listing()
role.
The following code instance shows us how we can search a list of dictionaries for a specific element with the filter()
and lambda
functions.
listOfDicts = [ { "name": "Tommy", "age": 20 }, { "proper name": "Markus", "age": 25 }, { "name": "Pamela", "age": 27 }, { "name": "Richard", "age": 22 } ] list(filter(lambda detail: item['name'] == 'Richard', listOfDicts))
Output:
[{'age': 22, 'name': 'Richard'}]
We searched the listing of dictionaries for the element where the proper noun
key is equal to Richard
by using the filter()
function with a lambda
role. Outset, nosotros initialized our list of dictionaries, listOfDicts
, and used the filter()
office to search the values that lucifer the lambda
part lambda item: item['proper noun'] == 'Richard'
in it. Finally, we used the list()
function to convert the results into a listing.
Employ List Comprehension to Search a List of Dictionaries in Python
List comprehension is a relatively shorter and very graceful fashion to create lists that are to be formed based on the given values of an already existing list.
We tin can use list comprehension to return a listing that produces the results of the search of the list of dictionaries in Python.
The following code uses list comprehension to search a listing of dictionaries in Python.
lstdict = [ { "name": "Klaus", "historic period": 32 }, { "proper name": "Elijah", "age": 33 }, { "name": "Kol", "age": 28 }, { "proper noun": "Stefan", "age": 8 } ] print([x for x in lstdict if 10['proper name'] == 'Klaus'][0])
Output:
{'name': 'Klaus', 'age': 32}
Write for us
DelftStack articles are written past software geeks similar you. If you too would like to contribute to DelftStack by writing paid manufactures, you tin check the write for us page.
Related Article - Python Lexicon
Related Article - Python List
Source: https://www.delftstack.com/howto/python/python-search-list-of-dictionaries/
0 Response to "Python Read List of Dictionaries From File"
Post a Comment