-2
dic = {"Name":"Test1","Age":"23"},{"Name":"Test2","Age":"24"},{"Name":"Test3","Age":"21"}

Search for Test3 and print 21

Dekel
  • 60,707
  • 10
  • 101
  • 129
user2311504
  • 35
  • 1
  • 6

2 Answers2

3

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"])
Rok Povsic
  • 4,626
  • 5
  • 37
  • 53
2

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
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126