Skip to content

Instantly share code, notes, and snippets.

@ericfrederich
Created October 28, 2015 20:09
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 ericfrederich/c8d8513b9790106d87b7 to your computer and use it in GitHub Desktop.
Save ericfrederich/c8d8513b9790106d87b7 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# based on slice example from
# https://medium.com/@tucnak/why-go-is-a-poorly-designed-language-1cc04e5daf2#.pr6pifnky
from itertools import count
def log(item, log_counter=count(start=1)):
print next(log_counter), item
if __name__ == '__main__':
# Some numbers, please!
numbers = [1, 2, 3, 4, 5]
log(numbers) # 1. [1 2 3 4 5]
log(numbers[2:]) # 2. [3 4 5]
log(numbers[1:3]) # 3. [2 3]
# Fun fact: you can use negative indices!
#
log(numbers[:-1]) # 4. [1 2 3 4]
# "Terrific" readability, Mr. Rossum! Well done!
#
# Now let's say I want to append six:
#
numbers.append(6)
log(numbers) # 5. [1 2 3 4 5 6]
# Let's now remove number 3 from it:
#
del numbers[2]
log(numbers) # 6. [1 2 4 5 6]
# Wanna insert some number? Don't worry, it is
# straight forward in Python!
#
numbers.insert(2, 3)
log(numbers) # 7. [1 2 3 4 5 6]
# In order to copy a slice, here is what you do:
#
copiedNumbers = numbers[:]
# or = list(numbers)
log(copiedNumbers) # 8. [1 2 3 4 5 6]
# And there's more.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment