2

I have a list and a dictionary and I want to replace the list item with the value of the dictionary. A simplified version of my problem is as follows:

#This is a list
a=['dog', 'cat', 'cow']

#This is a dictionary
b={'dog': 'barks', 'cat': 'meows', 'cow': 'moos' } 

Now list a[0] i.e dog is searched in the dictionary b and key dog will be found and I want its value barks in place of dog in the list a.

Expected output should be something like this

a=['barks','meows','moos']

I have been able to do this with the help of three text files in which one text file had the format 'key-value' and in other two I extracted key and values respectively with the help of split() function and then later matching the index position to get the desired result. But, the problem is that the code is way too long. I tried to do the above approach which I asked but couldn't get to replace the values.

Arpit Agarwal
  • 517
  • 1
  • 5
  • 17

3 Answers3

6

You can index out of the dictionary using a list comprehension

>>> a = [b[i] for i in a]
>>> a
['barks', 'meows', 'moos']
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • Maybe a quick mention of `dict.get` for the use of its default value? In case `a = ['dog', 'cat', 'archeopteryx']` and the dictionary is rightly confused about just what sound that dinosaur might make. – Adam Smith Jun 15 '15 at 18:00
  • I couldn't get it. Can you please elaborate a little? – Arpit Agarwal Jun 15 '15 at 18:00
  • @sarahjones it's a [list comprehension](https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions). Essentially it loops through every value in `a`, assigns each value to `i` (in turn), and builds a list out of the resultant `b[i]`s. – Adam Smith Jun 15 '15 at 18:09
  • @Adam Smith got it. I am new to python so I didn't knew much about list comprehension. – Arpit Agarwal Jun 15 '15 at 18:14
6

There probably are lots of ways to do that, here is one more:

map(b.get, a)

or

map(lambda x: b[x],a)

Link to the map doc: https://docs.python.org/2/library/functions.html#map

Alex Kreimer
  • 1,106
  • 2
  • 11
  • 25
  • 1
    +1 Although it's fair to note that this only gives you a list in Python2. In Python3 you'll have to do `a = list(map(b.get, a))` – Adam Smith Jun 15 '15 at 18:08
  • 1
    BTW the reason I asked you to change is explained [here](http://stackoverflow.com/questions/1247486/python-list-comprehension-vs-map) – Bhargav Rao Jun 15 '15 at 18:11
  • map function did not came in the basics I studied. What does this one do here? – Arpit Agarwal Jun 15 '15 at 18:21
  • @AdamSmith can you tell what b.get does and is there no need to write like b.get() i.e with brackets if its a function? – Arpit Agarwal Jun 15 '15 at 18:29
  • @AlexKreimer what does b.get do? – Arpit Agarwal Jun 15 '15 at 18:30
  • @sarahjones take a look here http://stackoverflow.com/questions/11041405/why-dict-getkey-instead-of-dictkey – Alex Kreimer Jun 15 '15 at 18:32
  • @AlexKreimer yeah i understood that it is a method but shouldn't there be brackets like b.get() and is it taking the input from a? – Arpit Agarwal Jun 15 '15 at 18:36
  • @sarahjones map expects a function as its first parameter, b.get() will cause .get method to be called before map, which is not what you want – Alex Kreimer Jun 15 '15 at 18:39
  • @AlexKreimer thanks for your insight. Understood completely. I am a newbie in python and its my first programming language ever, sorry if I irritated you. Any good resources can you recommend where I can practice more codes in python? – Arpit Agarwal Jun 15 '15 at 18:45
  • @sarahjones You did not! Good luck :) SO is so invaluable: http://stackoverflow.com/questions/70577/best-online-resource-to-learn-python – Alex Kreimer Jun 15 '15 at 18:47
  • 1
    @sarahjones Remember that in Python, everything is an object. `map` expects its first argument to be the *function object* to use. `b.get` is the function object, while `b.get(some_argument)` calls that function. `map` needs the function itself, not the result of the function being called. `map` works by calling its given function with every element in the second argument, so `map(foo, [1, 2, 3, 4, 5])` does `foo(1)` then `foo(2)` then `foo(3)` etc then returns an iterator that has the result of each, not unlike `[foo(1), foo(2), foo(3), ...]`. In Python2, it's LITERALLY that. – Adam Smith Jun 15 '15 at 19:41
  • @AdamSmith Sir, you gave the best explanation of this function. Nowhere I read explained with such simplicity. Thanks for your help. – Arpit Agarwal Jun 16 '15 at 16:49
3

If you want the list modified in-place:

a=['dog', 'cat', 'cow']
b={'dog': 'barks', 'cat': 'meows', 'cow': 'moos' }
for idx,value in enumerate(a):
    a[idx] = b[value]
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
  • 2
    `idx, val` might be more appropriate here. `val, item` seems ambiguous (but YMMV) – Adam Smith Jun 15 '15 at 17:58
  • @Adam Smith.Yeah this one is much more readable in terms of looking and easy to understand if one does not know about list comprehension. Thanks for the answer. – Arpit Agarwal Jun 15 '15 at 18:17