Last active
March 19, 2016 22:54
-
-
Save zulzeen/cbd5a09e3f4e5608db42 to your computer and use it in GitHub Desktop.
List comprehension to generate a list from a dict containing each key its value times (python)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bucket = {"red_ball": 1, "blue_ball": 2, "green_ball": 3} | |
balls = [k for k in bucket.keys() for _ in range(bucket.get(k))] | |
# Returns ['blue_ball', 'blue_ball', 'green_ball', 'green_ball', 'green_ball', 'red_ball'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment