Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 705
rɑːdʒɑ Avatar asked Feb 08 '16 05:02

rɑːdʒɑ


3 Answers

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

like image 52
Hackaholic Avatar answered Oct 19 '22 21:10

Hackaholic


print list by its key

print(b[0]['addr'])
like image 45
BMW Avatar answered Oct 19 '22 21:10

BMW


You can just use a print(b[0]['addr'])

like image 3
Morgan G Avatar answered Oct 19 '22 21:10

Morgan G