Python Lists

Posted on 1 Comment


Welcome to my Python lists reference guide. The objective is to gather essential information on the list data type in one place with lots of examples. Please let me know what you think…additions, corrections, and suggestions welcome!

create lists

  • lists are ordered sequences and can hold any combination or types of objects or expressions

    l = [student_1, 56754, 'hello']
    l = [[1,2,3], [4,5,6]]
    l = [1 == 2, 1 == 1] # l contains [False, True]
  • create an empty list

    l = []
    l = list() # the list constructor
  • create a list by applying a function to every member of a sequence, called a list comprehension

    l = [f(x) for x in sequence]
    l = [x*2 for x in range(1,6)] # [2,4,6,8,10]
    l = ["${:.2f}".format(x) for x in [1.10343,5.59730,4.35467]]
    # ['$1.10', '$5.60', '$4.35']
  • or by casting another sequence into a list type

    l = list('hello') # ['h','e','l','l','o']
    l = list((1,2,3)) # [1,2,3]

use lists

  • lists have a zero-based index, and slice notation is used to access one or more objects in the list

    l[0] # first item in list
    l[:2] # first two items in list; same as l[0:2]
    l[3:6] # the fourth, fifth, and sixth items
    l[-1] # last item in list
    l[5:] # the sixth item until the end; same as l[5:len(l)]
  • add a step parameter to get every second object, every third object, etc.

    l[::2] # every other item in list
    l[1:8:3] # every third item in slice [1:8]
  • index gets the index of the first occurrence of the argument

    l = ['hello','world', 'world']
    l.index('world') # returns 1
  • loop through lists using standard for-in loops

    for x in nums:
        print('x= ', x)
  • use the enumerate function to get the indices too

    for i, x in enumerate(nums):
        print('i= ', i, ' and x= ', x)
  • to test membership in a list, use the in operator for best performance

    if extension in ['jpg', 'png', 'svg']:
        print('graphics file')
  • use sort to sort a list in place and sorted to return a sorted copy of the list while leaving the original untouched

    l = [4,2,3,5,1]
    m = sorted(l) # l is still [4,2,3,5,1]
    l.sort() # now both m and l are [1,2,3,4,5]
  • other useful list functions

    len(l) # returns number of objects in list
    l.count(x) # returns a number of times x occurs in list
    max(l) and min(l) # return max/min of list
    sum(l) # returns sum of list of summable items
    any(l) # True if any members evaluate to True
    all(l) # True if all members evaluate to True

modify lists

  • slicing notation can be used to modify a list; just assign a value or sequence of values to the slice

    l = ['a','b','c','d','e']
    l[4] = 'z' # l is ['a','b','c','d','z']
    l[:2] = ['v', 'w'] # l is ['v','w','c','d','z']
    l[2:4] = ['x', 'y'] # l is ['v','w','x','y','z']
    • to add objects to a list use append, extend, or insert
      l.append('extra') # adds one element to end of list
      l.extend(['extra', 'large', 'fries']) # adds list of elements to end of list
      l.insert(i, x) # inserts element x at index i
  • to remove objects from a list, use del, remove, or pop

    del l[3] # delete fourth item in the list
    del l[3:5] # delete fourth and fifth item in the list
    l.remove(x) # remove first occurrence of x in the list
    l.pop() # delete and return last item in the list
    l.pop(i) # delete and return item at index i
  • you can use some operators on lists

    l = [1,2,3]
    l += [4] # concatenation - like l.extend([4]) except returns a new list
    l = [0, 1] * 3 # repetition - same as [0, 1] + [0, 1] + [0, 1] or [0, 1, 0, 1, 0, 1]

do stuff with lists

  • easily implement stack & queue data structures using the methods described above

    • stacks
      stack = [1,2,3]
      stack.append(4) # stack is [1,2,3,4]
      stack.pop() # returns 4; stack is [1,2,3]
    • queues
      queue = [3,2,1]
      queue.insert(0,4) # queue is [4,3,2,1]
      queue.pop() # returns 1; queue is [4,3,2]
    • note: inserting items at the beginning of the list is slow compared to the end of the list, so for best performance use Python’s deque class from the collections module for queues
  • string manipulation and analysis using lists

    s = 'the quick brown fox jumped over the lazy sleeping dog'
    l = s.split(' ') # l is ['the', 'quick', 'brown', 'fox', 'jumped', 'over', 'the', 'lazy', 'sleeping', 'dog']
    l = list(set(l)) # remove duplicates
    l.sort() # alphabetize
    print len(l) # print number of unique words in s
    print '\n'.join(l) # print unique words in s, one per line
  • zip two lists together into a list of tuples

    x_data = [1, 3, 5]
    y_data = [2, 4, 6]
    data_points = zip(x_data, y_data) # data_points is [(1, 2), (3, 4), (5, 6)]

list gotchas

  • lists as default arguments – default values are evaluated only once, so there are differences in using a mutable type (list, etc.) and an immutable type (None, etc.) as a default

    • this function will keep appending char to the same list on subsequent calls
      def add_char(char, l=[]):
          return l
      print add_char('*') # ['*']
      print add_char('*') # ['*', '*']
      print add_char('*') # ['*', '*', '*']
    • this function will create a new list on every call
      def add_char(char, l=None):
          if l is None:
              l = []
          return l
      print add_char('*') # ['*']
      print add_char('*') # ['*']
      print add_char('*') # ['*']
  • assigning a variable to a list does not make a copy of it

    l1 = [1,2,3]
    l2 = [4,5,6]
    l2 = l1 # now the list [1,2,3] is referenced by both l1 and l2
    # the list [4,5,6] is an object with no references and no way
    # to access it, so it will be automatically deleted (garbage collected)
    l1.append(4) # now the value of l1 and l2 is [1,2,3,4]
  • use the list constructor or slicing notation to copy a list

    l2=list(l1) # both of these make a copy of l1
    l2=l1[:] # and assign its reference to l2


1 comment

  1. Great info Jason. Thanks for providing it!

    Adding to your “list gotchas” section, if you are looping through a list and need to modify it based on some criteria (e.g. removing a value from the list if a business rule dictates it), then it’s best to start by looping though a copy of the list rather than the original itself. That way you can modify the original as you loop through the copy and the “pointer” doesn’t get messed up.

    for x in l2[:]:

Leave a Reply

Your email address will not be published. All fields are required.