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']
Related
This question already has answers here:
How to iterate over a python list?
(4 answers)
Closed 3 years ago.
I am trying to write a function in Python 3.6 that returns the content of a dictionary as a list, sorted according to the key in descending order.
Example test cases are as follows:
Inputs: {'y':1, 'z':1, 'x':1} | {3:['x','y'], 100:['z']}
Desired Outputs: [{'z':1},{'y':1},{'x':1}] | [{100:['z']}, {3:['x', 'y']}]
def sorted_dict_content(key_val):
# create an empty list
key_itemList = []
# for each key in sorted list of keys of key_val:
# append the key-value pair to key_itemList
for i in key_val:
key_itemList[key_val[i]].append(i)
sorted(key_val.key(), reverse=True)
return key_itemList
However, when I run the program I am getting IndexError: list index out of range. I also have not had luck with sorted().
Thanks for the help.
Trying to assign to the list via list[something] = something else will try to acces the list at the something-index not extend the list by adding the something-index, unlike a dict's behaviour. The correct way to append the key-value-pair to the list would be
key_ItemList.append({key_val[i]:i})
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 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?".
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 combine two lists into a dictionary in Python? [duplicate]
(6 answers)
Closed 7 years ago.
Is there any build-in function in Python that merges two lists into a dict? Like:
combined_dict = {}
keys = ["key1","key2","key3"]
values = ["val1","val2","val3"]
for k,v in zip(keys,values):
combined_dict[k] = v
Where:
keys acts as the list that contains the keys.
values acts as the list that contains the values
There is a function called array_combine that achieves this effect.
Seems like this should work, though I guess it's not one single function:
dict(zip(["key1","key2","key3"], ["val1","val2","val3"]))
from here: How do I combine two lists into a dictionary in Python?