Reputation: 61
I have this list of dictionaries
{'datasource': 'firewall.vip', 'css-class': 'ftnt-virtual-ip ftnt-color-0', 'name': '1.1.1.1--192.168.1.1 Port 3751'}
{'datasource': 'firewall.vip', 'css-class': 'ftnt-virtual-ip ftnt-color-0', 'name': '1.1.1.1--192.168.1.1 Port 3750'}
{'datasource': 'firewall.vip', 'css-class': 'ftnt-virtual-ip ftnt-color-0', 'name': '1.1.1.1--192.168.1.1 Port 3753'}
{'datasource': 'firewall.vip', 'css-class': 'ftnt-virtual-ip ftnt-color-0', 'name': '1.1.1.1--192.168.1.1 Port 3754'}
{'datasource': 'firewall.vip', 'css-class': 'ftnt-virtual-ip ftnt-color-0', 'name': '1.1.1.1--192.168.1.1 Port 80'}
{'datasource': 'firewall.vip', 'css-class': 'ftnt-virtual-ip ftnt-color-0', 'name': '1.1.1.1--192.168.1.1 Port 3756'}
I'm trying to pull all of the 'name' values out.
I've tried doing this:
new_list = []
parsed_lines = {}
for items in oldlist:
parsed_lines['name'] = items.get('name')
new_list.append(parsed_lines)
But it's coming out like this:
[{'name': '1.1.1.1--192.168.1.1 Port 3756'}, {'name': '1.1.1.1--192.168.1.1 Port 3756'}, {'name': '1.1.1.1--192.168.1.1 Port 3756'}, {'name': '1.1.1.1--192.168.1.1 Port 3756'}, {'name': '1.1.1.1--192.168.1.1 Port 3756'}, {'name': '1.1.1.1--192.168.1.1 Port 3756'}]
Upvotes: 0
Views: 25
Reputation: 10545
If you just want the name values in a new list, you can use a list comprehension:
new_list = [d.get('name') for d in oldlist]
Upvotes: 1