This question already has answers here:
How to return dictionary keys as a list in Python?
(13 answers)
Closed 5 years ago.
list=['Mary','Bob','Linda']
dictionary={0:'Mary', 1: 'Anna', 2:'Bob', 3:'Alice', 4: 'Linda'}
if list in set(dictionary.values()):
name = dictionary.get(None, list)
values = itemgetter(!*dic)(dictionary)
How can I get keys from the dictionary as the code find out same values from the list and dictionary?
Second question: how can I get different values keys of the dictionary between a list and a dictionary? E,g: print out the keys are [1],[3]
Thank you
I'm surprised you didn't try the obvious:
dictionary.keys()
Note: this was provided when the question explicitly asked "How to get all keys from the dictionary?".
Related
This question already has answers here:
List of unique dictionaries
(23 answers)
how to uniqify a list of dict in python
(6 answers)
Closed 2 years ago.
I have a list of dictionaries and I would like to remove all dicts that are duplicates in number. Keep only the last!
Example:
List of dicts:
student_data = [{'number':'1234', 'url':'www.abc.com'}, {'number':'9999', 'url':'www.abc.com'}, {'number':'1234', 'url':'www.xyz.com'}]
As you can see in we have 2 dicts where number is 1234. Therefore I remove the first dict and keep the last:
List of dicts:
student_data = [{'number':'9999', 'url':'www.abc.com'}, {'number':'1234', 'url':'www.xyz.com'}]
Any idea how to do this?
This question already has answers here:
Convert a python dict to a string and back
(12 answers)
Closed 3 years ago.
I have a predefined dictionary, and I want to be able to search the keys and values of the dictionary for a target string.
For example, if this is my dictionary:
my_dict = {u'GroupName': 'yeahyeahyeah', u'GroupId': 'sg-123456'}
I want to be check whether 'yeahyeahyeah' or 'GroupId' are in the keys or values of the dictionary. I think I want to convert the entire dictionary into a string, so that I can just do a substring search, but am open to other ideas.
EDIT: taking advice from comments
Here's how to create a list from the key value pairs in your dictionary.
my_dict = [{u'GroupName': 'yeahyeahyeah', u'GroupId': 'sg-123456'}]
my_list = list(my_dict[0].keys()) + list(my_dict[0].values())
This returns:
['GroupName', 'yeahyeahyeah', 'GroupId', 'sg-123456']
This question already has an answer here:
How to loop over a list of dicts and print values of a specific key?
(1 answer)
Closed 4 years ago.
I have a json file which contains a list with many dictionaries. Here I've included just two. But I want to iterate through all of the dictionaries and only get the code value (Company1 and Company2).
[{"code":"Company1","exchange_short_name":"ST","date":"2000-01-01"},
{"code":"Company2","exchange_short_name":"ST","date":"2000-01-01"}]
I've gotten to this, but it gives me all of the values inside the dictionary, and not the code values.
for d in jsonData:
for key in d:
print(d[key])
[item['code'] for item in jsonData]
will return list of codes.
This question already has an answer here:
python: get the most frequent value in a list of dictionaries
(1 answer)
Closed 7 years ago.
For example,
I have
myList = [{'imdb' : '12345'...}, {'imdb' : '54234'....}, {'imdb' : '12345'...}...]
I want
myList = [{'imdb' : '12345'...}, {'imdb' : '12345'...}...]
I want to get the most common imdb key value.
Thanks.
There is one question which answers how to get the most common list item, but I want the most common key value of dictionaries in a list.
This is sort of different.
from collections import Counter
most_common_imdb_value = Counter(d['imdb'] for d in myList).most_common(1)[0]
If you then need a list of those dictionaries that match the most common imdb value do:
[d for d in myList if d['imdb'] == most_common_imdb_value]
This question already has answers here:
How do I sort a dictionary by value?
(34 answers)
Closed 8 years ago.
I have a dictionary like this.
d = {'"mn': '800"', '"kl': '700"', '"ng': '1100"'}
How to sort this into descending order. like below
ng,1100
mn,800
kl,700
You can user OrderedDict for that.
reverse_dictionary = OrderedDict(reversed(sorted(d.items())), key=lambda t: t[1])
Im not sure what you mean, a dictionary by definition is unsorted. you may want to use an array of dictionaries.