Python Forum

Full Version: How to seperate dict value?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
How can i seperate ip address and mac addresses??


['192.168.1.240,fe80::350e:d28d:14a5:5cbb']
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.
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'