Created
February 23, 2017 15:57
-
-
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.
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
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