pythonpython-netifaces

How to get data from list with dictionary


Hello I have a python variable with List plus dictionary

 >>> print (b[0])
    {'peer': '127.0.0.1', 'netmask': '255.0.0.0', 'addr': '127.0.0.1'}
-----------------------------------------------------------------------
   >>> print (b)
[{'peer': '127.0.0.1', 'netmask': '255.0.0.0', 'addr': '127.0.0.1'}]
>>>

I have tried everything But I couldn't get 'addr' extracted.

Help Please.


Solution

  • try this:

    print (b[0]['addr'])
    

    print(b[0]) gives a dictionary, in dictionary you can fetch the value by its key like dict[key] => returns its associated value.

    so print(b[0]['addr']) will give you the value of addr

    Read about python data structure here Data structure