Python Forum

Full Version: problem with lambda and list compression
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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)
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.