Python Forum
Get New List Based on Dictionary Key
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get New List Based on Dictionary Key
#1
I'm new to Python. If I have a list indicate the order for a new list with the key in a dictionary, how could I get the new list with the values in the dictionary.

For instance:

l = ['a','b','a','c','b']
D = {'a':[3,2],'b':[4,5],'c':[1]}

How can I get a new list as:
L = [3,4,2,1,5]


A lot of thanks!
Reply
#2
What have you tried? We're not big on writing code for people, but we're big on helping people with code they've written.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Here is my code. It gives me the wanted list, but the two "for" loops is a little redundant. I'm wondering if there is simpler way to code it out.

l = ['a','b','a','c','b']
D = {'a':[3,2],'b':[4,5],'c':[1]}


L = []

for i in range(len(l)):
    for k in D:
        if l[i] == k:
            L.append(D[k][0])
            D[k].remove(D[k][0])
Thanks!
Reply
#4
something like
my_list= ['a','b','a','c','b']
my_dict = {'a':[3,2],'b':[4,5],'c':[1]}
result = [my_dict[key].pop(0) for key in my_list]
print(result)
but it assumes all elements in my_list are present in my_dict.keys and there are enough elements in the respective list in the dict.
Otherwise
my_list= ['a','b','a','c','b']
my_dict = {'a':[3],'b':[4,5],'c':[1]}
result = []
for key in my_list:
    try:
        result.append(my_dict[key].pop(0))
    except (KeyError, IndexError):
        continue
print(result) 
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Awesome, thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to remove all elements from list based on a condition sg_python 3 423 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  Dictionary in a list bashage 2 539 Dec-27-2023, 04:04 PM
Last Post: deanhystad
  filtering a list of dictionary as per given criteria jss 5 666 Dec-23-2023, 08:47 AM
Last Post: Gribouillis
  Sort a list of dictionaries by the only dictionary key Calab 1 488 Oct-27-2023, 03:03 PM
Last Post: buran
  How to add list to dictionary? Kull_Khan 3 998 Apr-04-2023, 08:35 AM
Last Post: ClaytonMorrison
  check if element is in a list in a dictionary value ambrozote 4 1,962 May-11-2022, 06:05 PM
Last Post: deanhystad
  Dictionary from a list failed, help needed leoahum 7 1,955 Apr-28-2022, 06:59 AM
Last Post: buran
  select Eof extension files based on text list of filenames with if condition RolanRoll 1 1,507 Apr-04-2022, 09:29 PM
Last Post: Larz60+
  how to assign items from a list to a dictionary CompleteNewb 3 1,564 Mar-19-2022, 01:25 AM
Last Post: deanhystad
  Python, how to manage multiple data in list or dictionary with calculations and FIFO Mikeardy 8 2,595 Dec-31-2021, 07:47 AM
Last Post: Mikeardy

Forum Jump:

User Panel Messages

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