3

I have a list of values that match with certain keys from a dictionary I created earlier.

myDict = {1:'A',2:'B',3:'C'}
myList = ['A','A','A','B','B','A','C','C']

How can I create/convert myList into something like:

myNewList = [1,1,1,2,2,1,3,3]

Could someone point me in the right direction?

Not sure if it matters, I created the dictionary using json in another script, and I am now loading the created dictionary in my current script.

Mike Issa
  • 295
  • 2
  • 13
  • 3
    How have you guaranteed that `myDict`'s values are unique? If you haven't, what do you intend to do with a dictionary like `{1:'A', 2:'B', 3:'A'}`? Would you replace each `'A'` in `myList` with a `1` or a `3`... or raise an error of some sort? – Kevin J. Chase Dec 17 '15 at 18:46
  • 1
    In my case, the values are unique. Not sure if I will come across your scenario anytime soon, but for now I can make this code compile the way I want, which is awesome! Thanks for checking though! :) – Mike Issa Dec 17 '15 at 21:31

3 Answers3

8

One easy way is to just invert myDict and then use that to map the new list:

myNewDict = {v: k for k, v in myDict.iteritems()}
myNewList = [myNewDict[x] for x in myList]

Also take a look at this for Python naming conventions: What is the naming convention in Python for variable and function names?

Community
  • 1
  • 1
CookieOfFortune
  • 13,836
  • 8
  • 42
  • 58
  • 1
    If `myDict` contains duplicate values, this will result in a mostly-unpredictable `myNewDict` and `MyNewList`. O.P. hasn't addressed that situation yet... for all we know, unique values are already guaranteed. – Kevin J. Chase Dec 17 '15 at 18:52
  • 1
    @KevinJ.Chase honestly I think OP might have his keys and values flipped. But I think the succinct answer works for this situation unless more details are provided. – CookieOfFortune Dec 17 '15 at 19:07
1
myDict = {1:'A',2:'B',3:'C'}

myList = ['A','A','A','B','B','A','C','C']

value = [key for i in myList for key, value in myDict.iteritems() if i == value]

print value

I recommend you read a bit about the compression list http://docs.python.org.ar/tutorial/3/datastructures.html

  • 2
    Do you mean [list comprehensions](https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions)? – Kevin J. Chase Dec 17 '15 at 19:27
  • 2
    list comprehensions are great but using multiple list comprehensions together makes it hard to read and interpret it for humans. I would break it into two parts and solve it. – rajesh.kanakabandi Dec 17 '15 at 20:12
0

This works too:

myNewList = []
for val in myList:
    for key, value in myDict.iteritems():
        if val == value:
        myNewList.append(key)
user61026
  • 46
  • 3
  • 1
    If `myDict` contains duplicate values, this will append _all_ matching keys to `myNewList`, in a mostly-unpredictable order. – Kevin J. Chase Dec 17 '15 at 18:55
  • 1
    add break in the if clause to avoid the duplicates: `myNewList = []` `for val in myList:` ` for key, value in myDict.iteritems():` ` if val == value:` ` myNewList.append(key)` ` break` ` – user61026 Dec 17 '15 at 19:00