Skip to content

Instantly share code, notes, and snippets.

@jjsanderson
Created February 23, 2017 15:57
Show Gist options
  • Save jjsanderson/bc5651788139b005c35189639edbe2db to your computer and use it in GitHub Desktop.
Save jjsanderson/bc5651788139b005c35189639edbe2db to your computer and use it in GitHub Desktop.
Demo Python to put data into randomly-selected, previously-empty element of a source list.
import random
# 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
emptylist = [i for i, element in enumerate(source) if element == 0]
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