1

I have dictionary in the following format stored inside a list.Below are listed the 12 members (dictionaries) of list switch_ports

[ {'type': 'port', 'number': 1, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:011', 'desc': 's1-eth1'}, {'type': 'port', 'number': 2, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:012', 'desc': 's1-eth2'}, {'type': 'port', 'number': 1, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:021', 'desc': 's2-eth1'}, {'type': 'port', 'number': 2, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:022', 'desc': 's2-eth2'}, {'type': 'port', 'number': 1, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:031', 'desc': 's3-eth1'}, {'type': 'port', 'number': 2, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:032', 'desc': 's3-eth2'}, {'type': 'port', 'number': 1, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:041', 'desc': 's4-eth1'}, {'type': 'port', 'number': 2, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:042', 'desc': 's4-eth2'}, {'type': 'port', 'number': 1, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:051', 'desc': 's5-eth1'}, {'type': 'port', 'number': 2, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:052', 'desc': 's5-eth2'}, {'type': 'port', 'number': 1, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:061', 'desc': 's6-eth1'}, {'type': 'port', 'number': 2, 'port_state': 1, 'state': 'active', 'port_id': '00:00:00:00:00:00:02:062', 'desc': 's6-eth2'}] 

From the list switch_ports I want to access port_id above. How do I do that?

If I do

for port in switch_ports: print(port) 

I get the same output as above. However If I try to access individual key-value pairs as below.

for port in switch_ports: print(port[port_id]) 

How do I get the port_id (e.g 00:00:00:00:00:00:02:062) from the list switch_ports

2 Answers 2

1

You are almost there. Just use port_id as a string, like this

for port in switch_ports: print(port["port_id"]) 

If you want to gather all the port_ids as a list, then use list comprehension like this

port_ids = [port["port_id"] for port in switch_ports] 

Online Demo

Sign up to request clarification or add additional context in comments.

Comments

1

An alternative, use itemgetter from the operator module:

>>> import operator >>> operator.itemgetter('port_id') <operator.itemgetter object at 0x7f318d7b8b90> >>> port_id = operator.itemgetter('port_id') >>> for port in switch_ports: ... print port_id(port) ... 

prints:

00:00:00:00:00:00:02:011 00:00:00:00:00:00:02:012 00:00:00:00:00:00:02:021 00:00:00:00:00:00:02:022 00:00:00:00:00:00:02:031 00:00:00:00:00:00:02:032 00:00:00:00:00:00:02:041 00:00:00:00:00:00:02:042 00:00:00:00:00:00:02:051 00:00:00:00:00:00:02:052 00:00:00:00:00:00:02:061 00:00:00:00:00:00:02:062 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.