1

I have a list of dictionaries that is like this:

{'name': ['Sam'], 'where': ['London']},
{'name': ['Jon'], 'where': ['NY']},
{'name': ['Jon'], 'hobby': ['fifa']},
{'Age': ['20'], 'Country': ['US']},

I do have a dictionary like this that could have a lot of same keys but a lot doesn't have that key too. As in above example, we have name key in first 3, but we don't have name key in the 4th. I am trying to get a list of all values that has key 'name'.

Thanks

5 Answers 5

1

What you want is unclear (nor the link to or ).

If you simply want the values of name when those exist:

l = [{'name': ['Sam'], 'where': ['London']},
     {'name': ['Jon'], 'where': ['NY']},
     {'name': ['Jon'], 'hobby': ['fifa']},
     {'Age': ['20'], 'Country': ['US']},]

out = [d['name'] for d in l if 'name' in d]
# [['Sam'], ['Jon'], ['Jon']]

# or as flat list
out = [name for d in l if 'name' in d for name in d['name']]
# ['Sam', 'Jon', 'Jon']

# or just the first name
out = [d['name'][0] for d in l if 'name' in d]
# ['Sam', 'Jon', 'Jon']
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. However, I am getting this error: argument of type 'NoneType' is not iterable
Do you have a None in the list? please provide a reproducible list for which this error happens
there are errors in the list as well: This is some part form list of dictionary: ------------------------------------------------------------------- 0 {'select': ['all_cons_columns.column_name'], '... 1 {'select': ['free_mb'], 'where': ['name']} 2 {'insert': ['sessionid', 'entryid', 'statement... 3 error:MERGE /*+ OPT_PARAM('_parallel_syspls_ob... 4 error:MERGE /*+ OPT_PARAM('_parallel_syspls_ob... ----------------------------------------------------------------------- I am trying to get value paired with select key
I can't help you without a reproducible input.
0

Here is a way to extract all names:

data = [
    {'name': ['Sam'], 'where': ['London']},
    {'name': ['Jon'], 'where': ['NY']},
    {'name': ['Jon'], 'hobby': ['fifa']},
    {'Age': ['20'], 'Country': ['US']},
]

names = [item['name'][0] for item in data if 'name' in item]
print(names)

Result:

['Sam', 'Jon', 'Jon']

Comments

0

I assume your data is a list of dicts.

data = [{'name': ['Sam'], 'where': ['London']},
{'name': ['Jon'], 'where': ['NY']},
{'name': ['Jon'], 'hobby': ['fifa']},
{'Age': ['20'], 'Country': ['US']},]

You can get a sub list of all elements with a name key like this:

output = [elem for elem in data if elem.get("name")]

4 Comments

Thanks for your input, I get this error: AttributeError: 'NoneType' object has no attribute 'get'
Do you have a None in your input? Try this please output = [elem for elem in data if elem and elem.get("name")]
there were None, I removed them using: aaa= {k: v for k, v in df_1_joins_where.items() if v is not None} however now i get this error as I use that code: argument of type 'int' is not iterable
seems like your data is not in the same format as your example data
0

There are many ways to do this. One would be:

_list = [{'name': ['Sam'], 'where': ['London']},
{'name': ['Jon'], 'where': ['NY']},
{'name': ['Jon'], 'hobby': ['fifa']},
{'Age': ['20'], 'Country': ['US']}]

print(*(name for d in _list if (name := d.get('name'))))

Output:

['Sam'] ['Jon'] ['Jon']

Comments

0

You can try a list expression:

all_dicts = [{'name': ['Sam'], 'where': ['London']},
{'name': ['Jon'], 'where': ['NY']},
{'name': ['Jon'], 'hobby': ['fifa']},
{'Age': ['20'], 'Country': ['US']}]

dicts_with_name = [d.values() for d in all_dicts if 'name' in d.keys() ]

This extracts all values (for all keys) of those dictionaries in the list that have a name field. If you want the key, value pairs, you can d.items() instead of d.values()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.