dic = {"Name":"Test1","Age":"23"},{"Name":"Test2","Age":"24"},{"Name":"Test3","Age":"21"}
Search for Test3 and print 21
dic = {"Name":"Test1","Age":"23"},{"Name":"Test2","Age":"24"},{"Name":"Test3","Age":"21"}
Search for Test3 and print 21
You probably want your dictionaries to be inside of a list, like this:
dic = [{"Name":"Test1","Age":"23"},{"Name":"Test2","Age":"24"},{"Name":"Test3","Age":"21"}]
Now we can find Name
with value "Test3" and print Age
.
for d in dic:
if d["Name"] == "Test3":
print("Age is: " + d["Age"])
Based on the syntax you have given, python will treat it as tuple
of dict
objects. Let's see:
>>> dic = {"Name":"Test1","Age":"23"},{"Name":"Test2","Age":"24"},{"Name":"Test3","Age":"21"}
>>> type(dic)
<type 'tuple'> # type as "tuple"
>>> dic # lets print the content
({'Age': '23', 'Name': 'Test1'}, {'Age': '24', 'Name': 'Test2'}, {'Age': '21', 'Name': 'Test3'})
# ^ All "dict" objects wrapped in `(...)`
You need to just iterate over the tuple
(which are similar to list
as far as iteration is considered), and check for the Name
value as Test3
. Sample code:
>>> for item in dic:
... if item["Name"] == "Test3":
... print(item["Age"])
...
21