Home > Back-end >  How to extract only selected values from a dictionary?
How to extract only selected values from a dictionary?

Time:01-05

How to extract only the values for name, job and country

 identity: {'name': 'Emma', 'count': 56, 'job': 'Actress', 'country': 'UK'}
 Emma, an Actress, from UK

CodePudding user response:

If you want a string, more here

data = {'name': 'Emma', 'count': 56, 'job': 'Actress', 'country': 'UK'}
to_text = f'{data.get("name")} an {data.get("job")} from {data.get("country")}'

CodePudding user response:

I believe the reason your program returns not just the name, job and country, but also the count, is because you didn't apply a filter. You can try:

data = {'name': 'Emma', 'count': 56, 'job': 'Actress', 'country': 'UK'}
info = ['name', 'job', 'country']
print([data[v] for v in data if v in info])

Output:

['Emma', 'Actress', 'UK']
  •  Tags:  
  • Related