Skip to content

Instantly share code, notes, and snippets.

@Kalaborative
Last active October 16, 2016 07:37
Show Gist options
  • Save Kalaborative/bb131b7289903fcef0762da7413c5cb0 to your computer and use it in GitHub Desktop.
Save Kalaborative/bb131b7289903fcef0762da7413c5cb0 to your computer and use it in GitHub Desktop.
import sys
print "Type a list of numbers with 3 in the middle."
myl = input('> ')
n = []
# Find the index of 3.
idx = myl.index(3)
try:
n.append(myl.pop(idx)) # Removes '3' from the list and stores it in n
except AttributeError:
print "Your list did not include brackets"
sys.exit()
# Split the list at the index
foo = [myl[x:x + idx] for x in xrange(0, len(myl), idx)]
# Reverse the list of lists
foo.reverse()
# After the first list (at index 0), insert [3]
foo.insert(1, n)
# Flatten the list
bar = [item for sublist in foo for item in sublist]
# print the list
print bar
@Kalaborative
Copy link
Author

Note: does not work as expected when '3' is at index 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment