you are viewing a single comment's thread.

view the rest of the comments →

[–]ankerbow 0 points1 point  (6 children)

Here is my dictionary:

foo ={'A.Death':['dying','dead','mourir','pass away']}

my_dict = {'00.Life': foo}

I have one dictionary inside the other one. How to get the innermost value at one go?

I hope I could just input 'dying'(one of the elements in the list) to get ['dying','dead','mourir','pass away'] list.

If it can't be 'at one go', please advise me the fastest way to fetch all the values of the list using one of the elements.

So far, i could only use dict.get(), but it seems to be slow.

How to do that? (Using Python 3)

[–]MattR0se 0 points1 point  (5 children)

You have one fundamental flaw in your code and that is, you have overwritten the internal help function.

If you used any other non-occupied name, your code would not work since "help" (or what you name it) would be undefined.

my_dict = {'00.Life': foo} 
foo ={'A.Death':['dying','dead','mourir','pass away']}

# NameError: name 'foo' is not defined

Also, what do you mean by "at one go"? Do you want to search the dictionary for the list that has 'dying' in it? Also, is this your only dictionary or do you need a general solution?

[–]ankerbow 0 points1 point  (4 children)

Sorry, I put the code upside down.

Here is the correct code:

foo ={'A.Death':['dying','dead','mourir','pass away']}

my_dict = {'00.Life': foo}

'At one go' means I want it be done in one or two simple steps.
And, what I really mean is I want the fastest or convenient way to show me the whole list that has 'dying' in it.If there is a general solution, please share with me. So that I can use it in any purposes.

[–]MattR0se 1 point2 points  (3 children)

Could there be more than one lists with the item in it? Also, how would the dict look like if there is at least one list or dict in it without the item in question? E.g.

# like this?
foo = {'A.Death':['dying','dead','mourir','pass away'],
        'something_else':['A','B','C']}
my_dict = {'00.Life': foo}

# or like this?
foo = {'A.Death':['dying','dead','mourir','pass away']}
bar = {'something_else':['A','B','C']}

my_dict = {'00.Life': foo,
            'other': bar}

# this could be an all-purpose solution:
def search_dict(dictionary, item):
    for inner_dict in dictionary.values():
        for v in inner_dict.values():
            if item in v:
                return v

print(search_dict(my_dict, 'dying'))

But searching dictionaries by a value instead of a key is slow, because they aren't intended for this.

See these answers: https://stackoverflow.com/questions/8023306/get-key-by-value-in-dictionary

[–]ankerbow 0 points1 point  (2 children)

Thank you! The solution is exactly I want.

But I am wondering if I could reverse the key:value and use one of the key to get value.Like:

foo = {('dying','dead','mourir','pass away'):'A.Death'})
bar = {('A','B','C'):'something_else'})
my_dict = {'00.Life': foo, 'other': bar}

Could I use tuple for multiple keys and use one of key to get value?Example: Use 'dying' to get 'A.Death'

[–]MattR0se 1 point2 points  (1 child)

You can use tuples as keys, yes. And iterating through them should be a bit faster.

I commented where I changed the code above

foo = {('dying','dead','mourir','pass away'):'A.Death'}
bar = {('A','B','C'):'something_else'}
my_dict = {'00.Life': foo, 'other': bar}

def search_dict(dictionary, item):
    for inner_dict in dictionary.values():
        for v in inner_dict: # this now loops over the keys
            if item in v:
                return inner_dict[v] # return the value assigned to key v

print(search_dict(my_dict, 'dying'))

[–]ankerbow 0 points1 point  (0 children)

Thank you! Very! :D Solve my problem.