-1

Guys is there any way so that I can retrieve status value with mylist['status']

mylist = {
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Winnetka",
               "short_name" : "Winnetka",
               "types" : [ "locality", "political" ]
            },
         ],

   ],
   "status" : "OK"
}
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • Welcome to SO! Please correct your code. You have not provided valid python, so we cannot see how your data is structured. Also, please read: **[mcve]** – jpp Feb 20 '18 at 10:24
  • 1
    you are missing `}` after "address_components" list – Ami Hollander Feb 20 '18 at 10:24
  • 2
    You gave the name `mylist` to something that isn't a list. Prepare to get hurt in code review. – RemcoGerlich Feb 20 '18 at 10:31
  • Im pretty sure of my_list and its type print (type(my_list)) # print(my_list['status']) #TypeError: list indices must be integers or slices, not str – Von ClauseWitz Feb 20 '18 at 10:37
  • 1
    @VonClauseWitz Then the content of `myList` is __not__ what you paste here. What we’re seeing here is `myList` being a dictionary. We can't help further if what you’re asking is not the problem you’re having. – 301_Moved_Permanently Feb 20 '18 at 10:48

2 Answers2

3

Correct your code as follow (missing close parenthesis })

mylist = {
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Winnetka",
               "short_name" : "Winnetka",
               "types" : [ "locality", "political" ]
            },
         ],
      }
   ],
   "status" : "OK"
}

and afterwords you can call mylist['status']

> mylist['status'] 
OK
Ami Hollander
  • 2,435
  • 3
  • 29
  • 47
1

I've reformatted your code below so it may make more sense to you:

mylist = {"results" : [{"address_components" : [{"long_name" : "Winnetka",
                                                 "short_name" : "Winnetka",
                                                 "types" : ["locality", "political"]}]}],
          "status" : "OK"}

mylist['status']  # 'OK'
jpp
  • 159,742
  • 34
  • 281
  • 339