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
#1
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!!
Reply
#2
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.
Reply
#3
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?
Reply
#4
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
Reply
#5
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']
Reply
#6
>>> 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.
Reply
#7
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Dictionary in a list bashage 2 493 Dec-27-2023, 04:04 PM
Last Post: deanhystad
  How to access values returned from inquirer cspower 6 696 Dec-26-2023, 09:34 PM
Last Post: cspower
  filtering a list of dictionary as per given criteria jss 5 597 Dec-23-2023, 08:47 AM
Last Post: Gribouillis
  need to compare 2 values in a nested dictionary jss 2 796 Nov-30-2023, 03:17 PM
Last Post: Pedroski55
  Copying the order of another list with identical values gohanhango 7 1,060 Nov-29-2023, 09:17 PM
Last Post: Pedroski55
  Search Excel File with a list of values huzzug 4 1,147 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Sort a list of dictionaries by the only dictionary key Calab 1 452 Oct-27-2023, 03:03 PM
Last Post: buran
  Access list of dictionaries britesc 4 1,027 Jul-26-2023, 05:00 AM
Last Post: Pedroski55
  Comparing List values to get indexes Edward_ 7 1,082 Jun-09-2023, 04:57 PM
Last Post: deanhystad
  Printing specific values out from a dictionary mcoliver88 6 1,316 Apr-12-2023, 08:10 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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