Home > database >  I have a list with the curly brackets and I'm not sure how to search list with the curly bracke
I have a list with the curly brackets and I'm not sure how to search list with the curly bracke

Time:06-14

I would like to search for one of the specific ID's but not sure on how to navigate with the curly brackets

[{'address': '9 Lee Road, Wirral, Merseyside',
  'url': '/get/ODQ2MjhhNTg1Y2E1YzE2IDE3MDkxMzc2IDFkYTMyNmZkZWY3ZGMzNg==',
  'id': 'ODQ2MjhhNTg1Y2E1YzE2IDE3MDkxMzc2IDFkYTMyNmZkZWY3ZGMzNg=='},
 {'address': '9 Lee Road, Aylesbury, Buckinghamshire',
  'url': '/get/MTEwZDgzMGUxMDBlMWQyIDIyMDI5NjA1IDFkYTMyNmZkZWY3ZGMzNg==',
  'id': 'MTEwZDgzMGUxMDBlMWQyIDIyMDI5NjA1IDFkYTMyNmZkZWY3ZGMzNg=='},
 {'address': '9 Lee Road, London',
  'url': '/get/MjEyYTIxNDhjZjM5ZTQ4IDU3ODQ4NzUgMWRhMzI2ZmRlZjdkYzM2',
  'id': 'MjEyYTIxNDhjZjM5ZTQ4IDU3ODQ4NzUgMWRhMzI2ZmRlZjdkYzM2'},
 {'address': '9 Lee Road, Manchester',
  'url': '/get/MmNkNDQzN2I2ODc3NmVhIDMwMTUwOTg3IDFkYTMyNmZkZWY3ZGMzNg==',
  'id': 'MmNkNDQzN2I2ODc3NmVhIDMwMTUwOTg3IDFkYTMyNmZkZWY3ZGMzNg=='},
 {'address': '9 Lee Road, Aldeburgh, Suffolk',
  'url': '/get/N2YzZGJiMTQ5OGRlYjg3IDIyOTczNDM5IDFkYTMyNmZkZWY3ZGMzNg==',
  'id': 'N2YzZGJiMTQ5OGRlYjg3IDIyOTczNDM5IDFkYTMyNmZkZWY3ZGMzNg=='},
 {'address': '9 Lee Road, Chesterfield, Derbyshire',
  'url': '/get/MWI5MGFhNDY5MjcwNDUwIDcxMjg0MjggMWRhMzI2ZmRlZjdkYzM2',
  'id': 'MWI5MGFhNDY5MjcwNDUwIDcxMjg0MjggMWRhMzI2ZmRlZjdkYzM2'}]

CodePudding user response:

my_list = [{'address': '9 Lee Road, Wirral, Merseyside',
  'url': '/get/ODQ2MjhhNTg1Y2E1YzE2IDE3MDkxMzc2IDFkYTMyNmZkZWY3ZGMzNg==',
  'id': 'ODQ2MjhhNTg1Y2E1YzE2IDE3MDkxMzc2IDFkYTMyNmZkZWY3ZGMzNg=='},
 {'address': '9 Lee Road, Aylesbury, Buckinghamshire',
  'url': '/get/MTEwZDgzMGUxMDBlMWQyIDIyMDI5NjA1IDFkYTMyNmZkZWY3ZGMzNg==',
  'id': 'MTEwZDgzMGUxMDBlMWQyIDIyMDI5NjA1IDFkYTMyNmZkZWY3ZGMzNg=='},
 {'address': '9 Lee Road, London',
  'url': '/get/MjEyYTIxNDhjZjM5ZTQ4IDU3ODQ4NzUgMWRhMzI2ZmRlZjdkYzM2',
  'id': 'MjEyYTIxNDhjZjM5ZTQ4IDU3ODQ4NzUgMWRhMzI2ZmRlZjdkYzM2'},
 {'address': '9 Lee Road, Manchester',
  'url': '/get/MmNkNDQzN2I2ODc3NmVhIDMwMTUwOTg3IDFkYTMyNmZkZWY3ZGMzNg==',
  'id': 'MmNkNDQzN2I2ODc3NmVhIDMwMTUwOTg3IDFkYTMyNmZkZWY3ZGMzNg=='},
 {'address': '9 Lee Road, Aldeburgh, Suffolk',
  'url': '/get/N2YzZGJiMTQ5OGRlYjg3IDIyOTczNDM5IDFkYTMyNmZkZWY3ZGMzNg==',
  'id': 'N2YzZGJiMTQ5OGRlYjg3IDIyOTczNDM5IDFkYTMyNmZkZWY3ZGMzNg=='},
 {'address': '9 Lee Road, Chesterfield, Derbyshire',
  'url': '/get/MWI5MGFhNDY5MjcwNDUwIDcxMjg0MjggMWRhMzI2ZmRlZjdkYzM2',
  'id': 'MWI5MGFhNDY5MjcwNDUwIDcxMjg0MjggMWRhMzI2ZmRlZjdkYzM2'}]

my_search_id = 'MWI5MGFhNDY5MjcwNDUwIDcxMjg0MjggMWRhMzI2ZmRlZjdkYzM2'
count = 0

for ids in my_list:
    #print("ID: {}".format(ids["id"]))
    
    if ids["id"] == my_search_id:
        print("Found")
        print("List index: {}".format(count))
        print("Address: {}".format(ids["address"]))
        print("Url: {}".format(ids["url"]))
        print("Id: {}".format(ids["id"]))
    count  = 1

CodePudding user response:

Since it is list[dict], you can iterate the list like normal and access each item’s id.

for item in list: and then for each item you can access the id value like this item[‘id’]

CodePudding user response:

What you have is a list of dictionaries or JSON object. You can iterate the list normally by using a for loop. for item in list: and compare the item["id"] to the id you want to find e.g

myid  = "the id"
for item in mylist:
    if item["id"] == myid:
    print(item)    
  • Related