Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python nested lists join lookup reference list

New to python so apologies if this is trivial. I have a list

list = [3,1,0,2]

and a nested lookup

lookup = [[265,301,201],[225,302,191],[225,35,134],[28,82,158]]

I need to match every element in "list" with each corresponding element index in "lookup" and return the value of this element from "lookup". The result should be:

result = [
[28,82,158],
[225,302,191],
[265,301,201],
[225,35,134]
]
like image 674
Neuril Avatar asked Nov 21 '25 08:11

Neuril


1 Answers

You can use a list comprehension:

result = [lookup[i] for i in list]

(Note that you shouldn't call a variable list. It will shadow the builtin of the same name, and will lead to unexpected beahaviour sooner or later.)

like image 131
Sven Marnach Avatar answered Nov 23 '25 21:11

Sven Marnach



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!