Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
dictionaries
#3
As you know the key names, you can use a simple dictionary comprehension with a for loop to step through the list.

For example,

list_ = [{'Name': 'Station1', 'Id':1},
{'Name': 'Station2', 'Id':5},
{'Name': 'Station3', 'Id':3},
{'Name': 'Station4', 'Id':9},
{'Name': 'Station5', 'Id':2}]

new_dict = {dict_['Name']: dict_['Id'] for dict_ in list_}
print(new_dict)
I am trying to help you, really, even if it doesn't always seem that way
Reply


Messages In This Thread
dictionaries - by brecht83 - Sep-27-2018, 10:37 AM
RE: dictionaries - by buran - Sep-27-2018, 11:05 AM
RE: dictionaries - by gruntfutuk - Sep-27-2018, 11:12 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020