Python Forum
How to access specific values from a dict? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to access specific values from a dict? (/thread-24238.html)



How to access specific values from a dict? - t4keheart - Feb-05-2020

I'm working with a dictionary (full of dictionaries, I believe?).

Here it is:
{1005672: {4461048: {'body': 'Mnow test test', 'msg_id': 4461048},
           4620511: {'body': 'test sms,test sms', 'msg_id': 4620511},
           4620584: {'body': 'test sms', 'msg_id': 4620584},
           4646648: {'body': 'test sms', 'msg_id': 4646648},
           4646877: {'body': 'test sms', 'msg_id': 4646877},
           4671891: {'body': 'test sms', 'msg_id': 4671891},
           4697824: {'body': 'test sms', 'msg_id': 4697824},
           4700841: {'body': 'test sms', 'msg_id': 4700841},
           5149461: {'body': 'Is it?', 'msg_id': 5149461},
           5159744: {'body': 'end of day test', 'msg_id': 5159744},
           5159829: {'body': 'trying again and again', 'msg_id': 5159829},
           5327356: {'body': 'test for convo id', 'msg_id': 5327356},
           5327606: {'body': 'eventually this will work!', 'msg_id': 5327606},
           5327619: {'body': 'this will work i say!', 'msg_id': 5327619},
           5327736: {'body': 'testing again\r\n', 'msg_id': 5327736},
           5332767: {'body': 'testing testing 1..2...3...', 'msg_id': 5332767},
           5333204: {'body': 'testing the convID part...', 'msg_id': 5333204},
           5333257: {'body': 'take 2!', 'msg_id': 5333257},
           5333489: {'body': 'aaaaaaaaaaaand ACTION!', 'msg_id': 5333489},
           5337141: {'body': 'does it work?', 'msg_id': 5337141},
           5363443: {'body': 'Test', 'msg_id': 5363443},
           5365638: {'body': 'testing again', 'msg_id': 5365638},
           5366005: {'body': 'testing sms ID', 'msg_id': 5366005}}}
I need to write 2 functions:

One which will take this dictionary as input, and return the 'body' value for the last line in the dictionary that was input. Please note that this dictionary is dynamic, in that the last line will be different each time the function runs.

(In this instance, it should return 'testing sms ID)

The next function should be able to take a key as input (one of the numbers on the left) and return the corresponding 'body' value.

{for example, input 5333257 should return take 2!


Thank you


RE: How to access specific values from a dict? - micseydel - Feb-05-2020

(Feb-05-2020, 05:03 PM)t4keheart Wrote: I want to be able to pull specific lines, by referencing the associated numbers on the left...
Could you elaborate on this? I read your post 3 times trying to figure out your question, and this seems like it, but I don't know what it means, when it comes down to code.


RE: How to access specific values from a dict? - t4keheart - Feb-05-2020

(Feb-05-2020, 06:24 PM)micseydel Wrote:
(Feb-05-2020, 05:03 PM)t4keheart Wrote: I want to be able to pull specific lines, by referencing the associated numbers on the left...
Could you elaborate on this? I read your post 3 times trying to figure out your question, and this seems like it, but I don't know what it means, when it comes down to code.

I'm sorry if I wasn't clear. I will modify my post to try to be more direct about what I'm asking.

I need to write a function that takes the dictionary I posted in the original post as input, and returns only the last entry's 'body' value.
5366005: {'body': 'testing sms ID', 'msg_id': 5366005}}}
So, 'testing sms ID' would be returned in this case.

I would also like to write a function that can be passed the msg_id (the number on the left for each entry, or key) and return the corresponding 'body' value.

for example, input '5366005' would also return 'testing sms ID'.

I'm not quite sure what I'm working with here which has made it difficult to research solutions. Is that a dictionary of dictionarys? A dictionary of lists?

Thank you for your time.


RE: How to access specific values from a dict? - metulburr - Feb-05-2020

The way you have it is JSON format without any assignment. If you assign it to a variable you you can use it as a dictionary. Everything there is under the first key 1005672, so you would do this to get the sub key of 5366005 print(d[1005672][5366005])
d = {1005672: {4461048: {'body': 'Mnow test test', 'msg_id': 4461048},
           4620511: {'body': 'test sms,test sms', 'msg_id': 4620511},
           4620584: {'body': 'test sms', 'msg_id': 4620584},
           4646648: {'body': 'test sms', 'msg_id': 4646648},
           4646877: {'body': 'test sms', 'msg_id': 4646877},
           4671891: {'body': 'test sms', 'msg_id': 4671891},
           4697824: {'body': 'test sms', 'msg_id': 4697824},
           4700841: {'body': 'test sms', 'msg_id': 4700841},
           5149461: {'body': 'Is it?', 'msg_id': 5149461},
           5159744: {'body': 'end of day test', 'msg_id': 5159744},
           5159829: {'body': 'trying again and again', 'msg_id': 5159829},
           5327356: {'body': 'test for convo id', 'msg_id': 5327356},
           5327606: {'body': 'eventually this will work!', 'msg_id': 5327606},
           5327619: {'body': 'this will work i say!', 'msg_id': 5327619},
           5327736: {'body': 'testing again\r\n', 'msg_id': 5327736},
           5332767: {'body': 'testing testing 1..2...3...', 'msg_id': 5332767},
           5333204: {'body': 'testing the convID part...', 'msg_id': 5333204},
           5333257: {'body': 'take 2!', 'msg_id': 5333257},
           5333489: {'body': 'aaaaaaaaaaaand ACTION!', 'msg_id': 5333489},
           5337141: {'body': 'does it work?', 'msg_id': 5337141},
           5363443: {'body': 'Test', 'msg_id': 5363443},
           5365638: {'body': 'testing again', 'msg_id': 5365638},
           5366005: {'body': 'testing sms ID', 'msg_id': 5366005}}}

print(d[1005672][5366005])



RE: How to access specific values from a dict? - t4keheart - Feb-05-2020

metulburr- Wow, it's that easy huh! Thank you, that solves one of my issues.

So I'm realizing that in order to get the last key/value I would need to first convert to a list and sort.

x = sorted(dict.keys())[-1]
print(x)
however this returns the top level key 1005672.
Any way to apply this same logic to the sub-keys?


RE: How to access specific values from a dict? - t4keheart - Feb-05-2020

Alright, I found a working solution for me.
For this example, my dictionary gets produced and stored to an object called 'inbound_dict'.
The following code works to return just the latest dictionary added to the set.
Now I think I can just convert to string and strip off what I don't want.


def get_latest(cached):
    latest_key = max(cache.keys())
    return cache[latest_key]["body"]

...
...**code that generates the dictionary**
...

    cached=inbound_dict
    cached={k: get_latest(cached[k]) for k in cached.keys()}
    print(cached)



RE: How to access specific values from a dict? - metulburr - Feb-05-2020

You would apply the same
x = sorted(d[1005672].keys())[-1]
print(x)