Creating Lists


In [68]:
# This is some IPython %magic
%xmode plain


Exception reporting mode: Plain

In [69]:
# with initial values
m = ['foo', 1, object()]
print m


['foo', 1, <object object at 0x109584e80>]

In [70]:
n = list('foo', 1, object())


Traceback (most recent call last):

  File "<ipython-input-70-168349f6d691>", line 1, in <module>
    n = list('foo', 1, object())

TypeError: list() takes at most 1 argument (3 given)

In [71]:
n = list(['foo', 1234, object()])
print n


['foo', 1234, <object object at 0x109584e90>]

Modifying Values


In [72]:
m[0] = 'bar'
print m


['bar', 1, <object object at 0x109584e80>]

In [73]:
m.append('baz')
print m


['bar', 1, <object object at 0x109584e80>, 'baz']

In [74]:
q = ['y', 'z']
o = ['a', 'b', 'c']
o.append(q)
print o


['a', 'b', 'c', ['y', 'z']]

In [75]:
p = ['1', '2', '3']
p.extend(q)
print p


['1', '2', '3', 'y', 'z']

In [76]:
r = ['a', 'b']
r + q


Out[76]:
['a', 'b', 'y', 'z']

In [77]:
r * 3


Out[77]:
['a', 'b', 'a', 'b', 'a', 'b']

In [78]:
del(p[2])
print p


['1', '2', 'y', 'z']

Getting Data Out


In [79]:
print m[0]
print m[1]


bar
1

In [80]:
# indices are integers
m['nope']


Traceback (most recent call last):

  File "<ipython-input-80-bedbfd1087ba>", line 2, in <module>
    m['nope']

TypeError: list indices must be integers, not str

In [81]:
# out of bounds
m[9]


Traceback (most recent call last):

  File "<ipython-input-81-8f7640154332>", line 2, in <module>
    m[9]

IndexError: list index out of range

In [82]:
m[-3]


Out[82]:
1

Slices


In [83]:
from string import ascii_lowercase
letters = list(ascii_lowercase)
print letters


['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

In [84]:
print letters[0:6]


['a', 'b', 'c', 'd', 'e', 'f']

In [85]:
print letters[:13]
print letters[13:]


['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm']
['n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

In [86]:
print letters[-3:]
print letters[:-3]


['x', 'y', 'z']
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w']

In [87]:
print letters[-10:23]


['q', 'r', 's', 't', 'u', 'v', 'w']

In [88]:
print letters[:]


['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

In [89]:
print letters[::2]
print letters[0:7:3]


['a', 'c', 'e', 'g', 'i', 'k', 'm', 'o', 'q', 's', 'u', 'w', 'y']
['a', 'd', 'g']

Inspecting


In [90]:
'f' in letters


Out[90]:
True

In [91]:
'F' in letters


Out[91]:
False

In [92]:
letters.index('m')


Out[92]:
12

In [93]:
letters.index('M')


Traceback (most recent call last):

  File "<ipython-input-93-bd9b906fe917>", line 1, in <module>
    letters.index('M')

ValueError: 'M' is not in list

In [94]:
len(letters)


Out[94]:
26

Unpacking


In [97]:
a, b, c = [1, 2, 3]
print a
print b
print c


1
2
3

In [99]:
x = [10, 20, 30]
d, e, f = x
print d
print e
print f


10
20
30

In [101]:
sun, mon, tue, wed, thu, fri, sat = range(7) # <- actually an "iterator" in python 3
print sun
print sat


0
6