Introducing Python © Chirag Wazir 2002 Prev   Index   Next

Lists & Tuples

a = [1, 2, 3, ['a', 'b'], "numbers"]
      [1, 2, 3, ['a', 'b'], 'numbers']
a[1:3]=[4,5,6,7]
      [1, 4, 5, 6, 7, ['a', 'b'], 'numbers']
a[5][1]=('x', 'y', 'z')
      [1, 4, 5, 6, 7, ['a', ('x', 'y', 'z')], 'numbers']
a.append(10)
      [1, 4, 5, 6, 7, ['a', ('x', 'y', 'z')], 'numbers', 10]
len(a)
      8
min(a[:5]), max(a[:5])
      (1, 7)
a.reverse()
      [10, 'numbers', ['a', ('x', 'y', 'z')], 7, 6, 5, 4, 1]
a.sort()
      [1, 4, 5, 6, 7, 10, ['a', ('x', 'y', 'z')], 'numbers']
a.insert(3, "new")
      [1, 4, 5, 'new', 6, 7, 10, ['a', ('x', 'y', 'z')], 'numbers']
a.pop()  # returns 'numbers'
      [1, 4, 5, 'new', 6, 7, 10, ['a', ('x', 'y', 'z')]]
a.remove(6)
      [1, 4, 5, 'new', 7, 10, ['a', ('x', 'y', 'z')]]
a.index(10)
      5
a[:3] + a[-1]
      [1, 4, 5, 'a', ('x', 'y', 'z')]