Home > Net >  List Comprehension for List of Dictionary to get Values Separately for Each Key
List Comprehension for List of Dictionary to get Values Separately for Each Key

Time:06-03

I want to get the city names and their respective population in separate list from a given list of dictionary. I have achieved this using naive method and using map() function as well but I need it to be executed using List Comprehension technique. I have tried below code but it is not giving proper output. What modifications should I do, please comment. Thanks.

towns = [{'name': 'Manchester', 'population': 58241}, 
         {'name': 'Coventry', 'population': 12435}, 
         {'name': 'South Windsor', 'population': 25709}]

print('Name of towns in the city are:', [item for item in towns[item]['name'].values()])
print('Population of each town in the city are:', [item for item in towns[item]['population'].values()])

** Expected Output **

Name of towns in the city are: ['Manchester', 'Coventry', 'South Windsor']

Population of each town in the city are: [58241, 12435, 25709]

CodePudding user response:

try this :

towns = [{'name': 'Manchester', 'population': 58241}, 
         {'name': 'Coventry', 'population': 12435}, 
         {'name': 'South Windsor', 'population': 25709}]

print('Name of towns in the city are:',
      [town['name'] for town in towns])
print('Population of each town in the city are:',
      [town['population'] for town in towns])

output:

Name of towns in the city are: ['Manchester', 'Coventry', 'South Windsor']
Population of each town in the city are: [58241, 12435, 25709]

CodePudding user response:

Since towns is a list of dictionaries, using a list comprehension to iterate through it will return dictionary elements. To access the data stored within those dictionaries, you need to index the elements of the list comprehension as you would within a normal for loop:

towns = [{'name': 'Manchester', 'population': 58241}, 
         {'name': 'Coventry', 'population': 12435}, 
         {'name': 'South Windsor', 'population': 25709}]

names       = [item["name"] for item in towns]
populations = [item["population"] for item in towns]

print("Name of towns in the city are:", names)
print("Population of each town in the city are:", populations)

This is analogous to the following for loop:

towns = [{'name': 'Manchester', 'population': 58241}, 
         {'name': 'Coventry', 'population': 12435}, 
         {'name': 'South Windsor', 'population': 25709}]

names       = []
populations = []

for town in towns:

    names.append(town["name"])
    populations.append(town["population"])

print("Name of towns in the city are:", names)
print("Population of each town in the city are:", populations)

Both of which produce the same output:

Name of towns in the city are:  ['Manchester', 'Coventry', 'South Windsor']
Population of each town in the city are:  [58241, 12435, 25709]

CodePudding user response:

Using map() method

towns = [{'name': 'Manchester', 'population': 58241}, 
         {'name': 'Coventry', 'population': 12435}, 
         {'name': 'South Windsor', 'population': 25709}]

print('Name of towns in the city are:', list(map(lambda k:k['name'], towns)))
print('Population of each town in the city are:', list(map(lambda v:v['population'], towns)))

Output

Name of towns in the city are: ['Manchester', 'Coventry', 'South Windsor']

Population of each town in the city are: [58241, 12435, 25709]

  • Related