The most Pythonic way is probably a list comprehension:
[ d for d in dict_list if d["START"] == "Virginia" and d["END"] == "Boston" ]
As mgilson pointed out, if you are assuming that there's only one item in the list with that pair of locations, you can use next
with the same generator expression, instead of the brackets. That will return just the matching dict, rather than a one-item list containing it:
trip = next( d for d in dict_list
if d["START"] == "Virginia" and d["END"] == "Boston" )
Either way, the result references the same dict object(s) as the original list, so you can make changes:
trip["Num"] = trip["Num"] + 1
And those changes will be there when accessed through the original list:
print(dict_list[2]["Num"]) # 2
As Ashwini Chaudhary indicated in his answer, your search may itself be specified as a dict. In that case, the if
condition in the generator expression is a little different, but the logic is otherwise the same:
search = { "START": "Virginia", "END": "Boston" }
trip = next(d for d in dict_list if all(i in d.items() for i in search.items()))