Python Forum
problem with lambda and list compression
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problem with lambda and list compression
#1
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)
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  That compression method is not supported tester_V 9 4,120 Jan-31-2023, 07:05 PM
Last Post: tester_V
  Problem with "Number List" problem on HackerRank Pnerd 5 2,292 Apr-12-2022, 12:25 AM
Last Post: Pnerd
  compression module for lz4? Skaperen 1 1,695 Oct-27-2021, 01:47 PM
Last Post: DeaD_EyE
  List comprehension and Lambda cametan 2 2,373 Jun-08-2021, 08:29 AM
Last Post: cametan
  CPC File Format (Cartesian Perceptual Compression) - Can Python Convert / Handle Them PSKrieger 2 2,615 Nov-11-2020, 02:57 PM
Last Post: PSKrieger
  Sorting list of names using a lambda (PyBite #5) Drone4four 2 2,868 Oct-16-2020, 07:30 PM
Last Post: ndc85430
  Practice problem using lambda inside the class jagasrik 3 2,317 Sep-12-2020, 03:18 PM
Last Post: deanhystad
  Multiple lambda functions in zipped list not executing psolar 0 1,671 Feb-13-2020, 12:53 PM
Last Post: psolar
  Rebuild LZ compression from C code via Python's LZMA module TD2 0 1,942 Apr-25-2019, 02:42 PM
Last Post: TD2
  list func in lambda mepyyeti 1 3,033 Mar-10-2018, 09:07 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020