Python Forum
Dictionary using variables as a keys
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dictionary using variables as a keys
#1
Im trying to create a dicctionary using two lists (one as a key and the other one as a value) like this:

list1 = ['A4', 'A9', 'A2'] # keys
list2 = [27.8, 7.1, 68.04] # values

Diccionary : {'A4': [27.8], 'A9': [7.1], 'A2': [68.04]}

Finally I need to sort by value and take the minimum value and key:

In this case: {'A9':[7.1]}


Thanks in advance
Reply
#2
zip them, to create key-value pairs.
>>> keys = ['A4', 'A9', 'A2']
>>> values = [27.8, 7.1, 68.04]
>>> list(zip(keys, values))
[('A4', 27.8), ('A9', 7.1), ('A2', 68.04)]
>>> dict(zip(keys, values))
{'A9': 7.1, 'A4': 27.8, 'A2': 68.04}
>>>
Reply
#3
The solution above uses a dict, but does not sort on the values.
You can say:

>>> a=[1,2,3]
>>> b=[6,5,4]
>>> c=zip(a,b)
>>> c
[(1, 6), (2, 5), (3, 4)]
>>> d=sorted(c, key=lambda x: x[1]) # sort on value element
>>> d
[(3, 4), (2, 5), (1, 6)]
>>> e=dict(d)
>>> e
{1: 6, 2: 5, 3: 4}
Reply
#4
(Oct-09-2017, 10:33 PM)miltmobley Wrote:
>>> d
[(3, 4), (2, 5), (1, 6)]
>>> e=dict(d)
>>> e
{1: 6, 2: 5, 3: 4}

Ok, but that's not sorted by value, either.  And since dictionaries only maintain order in some versions of python, for some implementations of python, shouldn't you just assume all dictionaries are just unordered?
Reply
#5
list1 = ['A4', 'A9', 'A2'] # keys
list2 = [27.8, 7.1, 68.04] # values
d = {k:v for k,v in zip(list1, list2) if v==min(list2)}
print d
Output:
{'A9': 7.1}
Note that in case there are multiple min values in list2, the result will be dict with multiple elements
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I'm assuming you're asking about accessing nested dictionary keys and values in Pytho DavidSah 0 580 Dec-18-2025, 01:54 AM
Last Post: DavidSah
Question [SOLVED] Access keys and values from nested dictionary? Winfried 4 914 Nov-17-2025, 11:47 AM
Last Post: Winfried
  Building a dictionary .update entry from variables Curbie 5 2,547 Sep-03-2024, 07:31 PM
Last Post: Curbie
  Adding keys and values to a dictionary giladal 3 4,735 Nov-19-2020, 04:58 PM
Last Post: deanhystad
  access dictionary with keys from another and write values to list redminote4dd 6 5,958 Jun-03-2020, 05:20 PM
Last Post: DeaD_EyE
  Drop Keys From Dictionary donnertrud 8 6,527 May-30-2020, 11:39 AM
Last Post: DeaD_EyE
  Problem adding keys/values to dictionary where keynames = "property" and "value" jasonashaw 1 3,127 Dec-17-2019, 08:00 PM
Last Post: jasonashaw
  Checking if the combination of two keys is in a dictionary? mrsenorchuck 6 6,391 Dec-04-2019, 10:35 AM
Last Post: mrsenorchuck
  Retrieving dictionary keys within with another dictionay bazcurtis 8 5,457 Oct-29-2019, 10:06 PM
Last Post: bazcurtis
  json.dumps to keep dictionary keys batchenr 1 2,992 May-14-2019, 11:17 AM
Last Post: buran

Forum Jump:

User Panel Messages

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