Python Forum
How to seperate dict value? - 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 seperate dict value? (/thread-36628.html)



How to seperate dict value? - ilknurg - Mar-11-2022

How can i seperate ip address and mac addresses??


['192.168.1.240,fe80::350e:d28d:14a5:5cbb']



RE: How to seperate dict value? - menator01 - Mar-11-2022

Here is one way

for string in ['192.168.1.240,fe80::350e:d28d:14a5:5cbb']:
    new_string = string
    split_string = new_string.split(',')

print(f'ip -> {split_string[0]}')
print(f'mac -> {split_string[1]}')
Output:
ip -> 192.168.1.240 mac -> fe80::350e:d28d:14a5:5cbb
On a side note, this is a list. Not a dict.


RE: How to seperate dict value? - perfringo - Mar-11-2022

You have list with one string item in it. Obvious way would be splitting this item at comma and unpack:

>>> line = ['192.168.1.240,fe80::350e:d28d:14a5:5cbb']
>>> ip_addr, mac_addr = line[0].split(',')
>>> ip_addr
'192.168.1.240'
>>> mac_addr
'fe80::350e:d28d:14a5:5cbb'
If the result should be dictionary then one could do this:

>>> data = dict(zip(('ip_addr', 'mac_addr'), line[0].split(',')))
>>> data
{'ip_addr': '192.168.1.240', 'mac_addr': 'fe80::350e:d28d:14a5:5cbb'}
>>> data['ip_addr']
'192.168.1.240'