Python Forum

Full Version: access dictionary with keys from another and write values to list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello!

I have a list of keys from a dictionary and I want to these keys to get the values from another dictionary where the keys are the same and write that values to a list..
Is there any possibility to do this?

Thank you!!
dictionaries have a keys method
https://docs.python.org/2/library/stdtyp...#dict.keys Wrote:keys()
Return a copy of the dictionary’s list of keys. See the note for dict.items().
Loop through the keys from one dictionary to access the items of the other.
Thanks for the quick answer... can anyone give me a quick example how to loop over it?

The dictionary part of my code looks like this:


d11=dict()
for a in range(len(GN)):
    d11[a]=GN[a]


listOfKeys = list()
listOfItems = d11.items()
for item  in listOfItems:
    if item[1] == ' 11':
        listOfKeys.append(item[0])
        

d11x=dict()
for m in range(len(GN)):
    d11x[m]=x[m]
How can I get the corresponding values to a new list?
I had never done this before, but this is fun:
dict1 = {'red':3,'blue':4,'yellow':6}
dict2 = {'red': 'is a color', 'blue': 'is another color'}
    
lst = dict1.keys()
print(lst)

for l in lst:
    try:
        print(l, dict2[l])
    except:
        pass
Paul
dict1 = {'red':3,'blue':4,'yellow':6}
dict2 = {'red': 'is a color', 'blue': 'is another color'}

dict2_values = []
for key in dict1.keys():
    value = dict2.get(key)
    if value:
        dict2_values.append(value)

print(dict2_values)
Output:
['is a color', 'is another color']
>>> dict1 = {'red':3,'blue':4,'yellow':6}                                                                                             
>>> dict2 = {'red': 'is a color', 'blue': 'is another color'}     
>>> [v for k, v in dict2.items() if k in set(dict1).intersection(dict2)]
['is a color', 'is another color'] 
It's a bit pity, that the set does not preserve the order. But if the order is not relevant, the set solution is the best. One problem is there, you're creating over and over an intersection with two sets in your list comprehension.

dict1 = {'red': 3,'blue': 4,'yellow': 6}
dict2 = {'red': 'is a color', 'blue': 'is another color'}

# the method keys() return a set like object,
# but the order is kept until a set operation is applied
# then the order is lost.
same_keys = dict1.keys() & dict2.keys()
# the & operator is the intersection of two sets: 
# https://docs.python.org/3/library/stdtypes.html?intersection=set#frozenset.intersection
# intersection(*others)
# set & other & ...
#    Return a new set with elements common to the set and all others.

same_keys_preserved_order = [key for key in dict1.keys() if key in dict2]


# key order from same_keys_preserved_order
# putting value1 and value2 into a tuple
values_dict1_dict2 = [(dict1[key], dict2[key]) for key in same_keys_preserved_order]

# dict.items() return an iterator, which preserve the order
# same with dict.keys(), dict.values()
values1 = [value for key, value in dict1.items() if key in same_keys]
values2 = [value for key, value in dict2.items() if key in same_keys]

# same with a for-block
values1 = []
for key, value in dict1.items():
    if key in same_keys:
        values1.append(value)
You need to know which values from which dict you need.