Find a specific key and value from the dictionary, in python

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

Search for Test3 and print 21


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