CobaltRed
CobaltRed

Reputation: 37

Fetch value of specific key from nested dictionary and store values in separate variables

I have following dictionary:-

    dict={'m1': {'ip': '10.250.10.10', 'port': 6697}, 'm2': {'ip': '10.250.10.20', 'port': 6699}}

I need to store values of ip in variables i.e

a = 10.250.10.10

b = 10.250.10.20

Could anyone please let me know how I can achieve this?

Upvotes: 0

Views: 154

Answers (4)

Moinuddin Quadri
Moinuddin Quadri

Reputation: 48067

You can create a generator expression to fetch the 'ip', unpack it and store the values into individual variables like:

my_dict = {'m1': {'ip': '10.250.10.10', 'port': 6697}, 'm2': {'ip': '10.250.10.20', 'port': 6699}}

a, b = (v['ip'] for v in my_dict.values())

Here's a functional approach to achieve this using map() and operator.itemgetter() as:

from operator import itemgetter

a, b = map(itemgetter('ip'), my_dict.values())

Above solutions will give you a and b as:

>>> a
'10.250.10.10'
>>> b
'10.250.10.20'

From Python 3.7+, dictionary maintain the order of insertion, and hence value of a and b can be predicted. For older versions of Python, the order is arbitrary and hence the value of a and b can not be predicted.


If you are on Python version 3.6 or less, and order matters for you, you can sort the dictionary keys using sorted(), and get the values based on the sorted order of keys as:

sort_dict_values = [my_dict[key] for key in sorted(my_dict)]
# where `sort_dict_values` holds:
#     [{'ip': '10.250.10.10', 'port': 6697}, {'ip': '10.250.10.20', 'port': 6699}]

Now you can use sort_dict_values (instead of my_dict.values()) to perform earlier operations to fetch the 'ip' preserving the value of a and b as per your desired behaviour. For example:

a, b = map(itemgetter('ip'), sort_dict_values)
                            # ^ instead of `my_dict.values()`

Upvotes: 6

Emanuele
Emanuele

Reputation: 178

dict={'m1': {'ip': '10.250.10.10', 'port': 6697}, 'm2': {'ip': '10.250.10.20', 'port': 6699}}

a = dict["m1"]["ip"]
b = dict["m2"]["ip"]

print(a, b)

OUTPUT:

10.250.10.10 10.250.10.20

Upvotes: 1

Bill the Lizard
Bill the Lizard

Reputation: 405755

First, dict is a built-in function in Python, so I'd avoid using it as a variable name.

To get specific values from a dictionary, you can use the get function, which allows you to provide a default value in case the key you're asking for isn't defined. This prevents a KeyError that can be raised if you access dictionary elements using square-brackets.

d = {'m1': {'ip': '10.250.10.10', 'port': 6697}, 'm2': {'ip': '10.250.10.20', 'port': 6699}}

a = d.get('m1', {}).get('ip', 'not found')
b = d.get('m2', {}).get('ip', 'not found')
c = d.get('m3', {}).get('ip', 'not found')
print(a)
print(b)
print(c)

This code gets the m1 sub-dictionary, passing an empty dictionary as the default, then gets the ip value from that dictionary, passing 'not found' as the default ip.

This way, you will still have some value in each variable a, b, and c, no matter what the structure of the dictionary you start with. Note that the value of c will be 'not found', because the sub-dictionary m3 doesn't exist.

Upvotes: 2

stuckoverflow
stuckoverflow

Reputation: 712

dict = {'m1': {'ip': '10.250.10.10', 'port': 6697},
        'm2': {'ip': '10.250.10.20', 'port': 6699}}

a = dict['m1']['ip']
b = dict['m2']['ip']

Upvotes: 1

Related Questions