Alex McLean
Alex McLean

Reputation: 2764

Finding the dictionary a key value comes from

Lets say we have a list of dictionaries, ranging in the thousands. Every dictionary has the exact same keys, but different values.

Is there any way to lookup which dictionary a key value comes from, and then access different key values from that same dictionary?

For example, say you have a list containing every person in a large city, dictionary format, like so:

people_in_city = [

        {'name': 'Bob Jang',
        'age': 45,
        'sex': 'male'},
        {'name': 'Barb Esau',
        'age': 56,
        'sex': 'female'},
        etc.
        etc.

Now, you want to lookup the age of Bob Jang, but you only have his name. Is there anyway to get his corresponding age using this format?

Upvotes: 1

Views: 83

Answers (6)

TheBlackCat
TheBlackCat

Reputation: 10308

There is a python package called bidict that can do this. It provides a two-way dict, which allows you to get the key from the value or the value from the key. An example from the documentation:

>>> element_by_symbol = bidict(H='hydrogen')
>>> element_by_symbol['H']  # forward mapping works just like with dict
'hydrogen'
>>> element_by_symbol[:'hydrogen']  # use slice for the inverse mapping
'H'

Upvotes: 0

cyneo
cyneo

Reputation: 946

I would suggest looking at a database as Adam Smith suggested. Though I would also like to suggest looking at a class. That would go something like:

class Person():
    def __init__(self, name, age, gender):
        self.name = name
        self.age = age
        self.gender = gender

people_in_city = []

def add_person(name, age, gender
               people_in_city=people_in_city):
    people_in_city.append(Person(name, age, gender))

def find_by_name(name):
    for person in people_in_city:
        if person.name == name:
            return person

Not the most elegant way, but it gets the job done, plus you can add more information and not have have to change the nature of your search function. So lets say you find dear 'Bob Jang', and you realize that you want to know that job he is doing (assuming you coded that into the class). You just do:

person_of_interest = find_by_name('Bob Jang')
person_of_interest.job
person_of_interest.age

Note that this only gives the LAST found value of the name, and not everyone with that name. Other methods will have to be employed for that. This method also means that you are holding all the information in a list, and that might get slow as the list grows. That is why databases would work better as your list grows.

And as a bonus, it is possible to create each person in parallel.

Upvotes: 1

pacifier
pacifier

Reputation: 181

 people_in_city = [

    {'name': 'Bob Jang',
    'age': 45,
    'sex': 'male'},
    {'name': 'Barb Esau',
    'age': 56,
    'sex': 'female'}]
 v = age or name sex etc..
 for i in people_in_city:
     if v in i.values():
         print i

Upvotes: 0

emvee
emvee

Reputation: 4449

(age,) = [v['age' ]for (k,v) in people_in_city.iteritems() if v['name']=="Bob Jang"]

Upvotes: 0

pnv
pnv

Reputation: 3135

Try this,

provided_name = 'some name'

persons_list_with_name = [person_info in person_info in people_in_city if person_info['name'] == provided_name]

for person_info in persons_list_with_name:
    print person_info['name'], person_info['age']

Upvotes: 0

Adam Smith
Adam Smith

Reputation: 54213

There's no fast way to do this in Python. I'd suggest something like:

def get_dict_from_key(list_of_dicts, key, value):
    return next((d for d in list_of_dicts if d[key] == value))

result_d = get_dict_from_key(dicts, 'name', 'Bob Jang')
result_d['age']

That said, this is the kind of thing that relational databases are made for! Learn SQL and use it :)

Upvotes: 3

Related Questions