Skip to content

Instantly share code, notes, and snippets.

@zulzeen
Last active March 19, 2016 22:54
Show Gist options
  • Save zulzeen/cbd5a09e3f4e5608db42 to your computer and use it in GitHub Desktop.
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)
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