Python Forum
access dictionary with keys from another and write values to list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
access dictionary with keys from another and write values to list

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 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:

for a in range(len(GN)):

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

for m in range(len(GN)):
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()

for l in lst:
        print(l, dict2[l])
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:

['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'] 
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
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: 
# 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:
You need to know which values from which dict you need.
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die:
All humans together. We don't need politicians!

Possibly Related Threads…
Thread Author Replies Views Last Post
  Class-Aggregation and creating a list/dictionary IoannisDem 1 172 Oct-03-2021, 05:16 PM
Last Post: Yoriz
  How to pass list of values to a API request URL chetansaip99 0 472 Sep-28-2021, 07:37 AM
Last Post: chetansaip99
  Compile list of dictianories out of another list of dictianories by certain keys CatorCanulis 10 1,082 Jun-10-2021, 08:35 PM
Last Post: perfringo
  Getting values from a dictionary brunolelli 5 1,071 Mar-31-2021, 11:57 PM
Last Post: snippsat
  Python dictionary with values as list to CSV Sritej26 4 910 Mar-27-2021, 05:53 PM
Last Post: Sritej26
  Conceptualizing modulus. How to compare & communicate with values in a Dictionary Kaanyrvhok 7 1,223 Mar-15-2021, 05:43 PM
Last Post: Kaanyrvhok
  convert List with dictionaries to a single dictionary iamaghost 3 942 Jan-22-2021, 03:56 PM
Last Post: iamaghost
  How to discard list repeat values akanowhere 7 1,134 Dec-28-2020, 09:14 PM
Last Post: akanowhere
Sad Convert python list to dictionary akanowhere 6 1,185 Dec-27-2020, 09:26 PM
Last Post: Pedroski55
  Adding keys and values to a dictionary giladal 3 809 Nov-19-2020, 04:58 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020