Python Forum
problem with lambda and list compression - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: problem with lambda and list compression (/thread-12038.html)



problem with lambda and list compression - Prince_Bhatia - Aug-06-2018

hi,

i am trying to insert random colors from a list into another list, but facing a bit problem, as it is returning lambda store place where it is stored not the colors.

How can i print the colors
import random
colors = ["red", "black", "green"]

dc = [(lambda a: random.choice(a)) for a in colors]
print(dc)



RE: problem with lambda and list compression - ichabod801 - Aug-06-2018

You don't need the lambda:

dc = [random.choice(colors) for color in range(10)]
The first part of the list comprehension is evaluated for each pass of the loop. Note that in your code, it is just evaluating the creation of a lambda.