Is there a more efficient and/or python way to make the following list conversion using a dictionary?
d = {1:2, 2:3, 3:4}
e = [1 , 1, 2, 2, 3, 1]
f=[]
for l in e:
f.append(d[l])
f
Is there a more efficient and/or python way to make the following list conversion using a dictionary?
d = {1:2, 2:3, 3:4}
e = [1 , 1, 2, 2, 3, 1]
f=[]
for l in e:
f.append(d[l])
f
Use a map (Efficient)
f = map(d.get, e)
For larger data set and complex manipulations use pandas
>>> import pandas as pd
>>> s = pd.Series(e)
>>> s.map(d)
f = [d[key] for key in e]
For a list of 1000 items,
List Comprehension: 2.346038818359400 × 10-4
Map: 9.059906005859375 × 10-6