Skip to content

Instantly share code, notes, and snippets.

@gvx
Created January 30, 2009 16:44
Show Gist options
  • Save gvx/55135 to your computer and use it in GitHub Desktop.
Save gvx/55135 to your computer and use it in GitHub Desktop.
Functions for lists
#Listtools 0.1
#By Robin Wellner (gvx)
#Functions for lists
#I hereby waive copyright and related or neighboring rights to this work
#See the Creative Commons Zero Waiver at <http://creativecommons.org/publicdomain/zero/1.0/>
def append(lst, object):
"listtools.append(lst, object) -- append object to end"
lst.append(object)
return lst
def extend(lst, iterable):
"listtools.extend(lst, iterable) -- extend list by appending elements from the iterable"
lst.extend(iterable)
return lst
def insert(lst, index, object):
"listtools.insert(lst, index, object) -- insert object before index"
lst.insert(index, object)
return lst
def remove(lst, value):
"listtools.remove(lst, value) -- remove first occurrence of value"
lst.remove(value)
return lst
def reverse(lst):
"listtools.reverse(lst) -- reverse *IN PLACE*"
lst.reverse()
return lst
def sort(lst, cmp=None, key=None, reverse=False):
"listtools.sort(lst, cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*"
lst.sort(cmp, key, reverse)
return lst
def setitem(lst, item, value, default=None):
"listtools.setitem(lst, item, value, default=None) -- sets lst[item] to value, but creates extra items as needed"
lst.extend(default for newitem in range(len(lst), item + 1))
lst[item] = value
return lst
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment