Skip to content

Instantly share code, notes, and snippets.

@jjsanderson
Created February 23, 2017 16:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jjsanderson/3b186ec0faa2937b6de61dabd75685a1 to your computer and use it in GitHub Desktop.
Save jjsanderson/3b186ec0faa2937b6de61dabd75685a1 to your computer and use it in GitHub Desktop.
Less (?) elegant way of finding empty indices of source list, using itertools
import random
from itertools import compress, count
# Initial list of references
source = [0, 1, 3, 4, 0, 5, 7, 0]
print source
# Build list of indices of source where content is zero
empty = compress(count(), [not x for x in source])
emptylist = list(empty)
print emptylist
# Select one of those indices at random
select = random.choice(emptylist)
print select
# Assign a value to the previously-empty element of the source list
source[select] = "TADA!"
print source
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment