Lists

Lists are sequences that hold heterogenous data types that are separated by commas between two square brackets. Lists have zero-based indexing, which means that the first element in the list has an index on '0', the second element has an index of '1', and so on. The last element of the list has an index of 'N-1' where N is the length of the list.


In [4]:
# import thr random numbers module. More on modules in a future notebook
import random

Define a list


In [2]:
# empty list
a = list()
# or
a = []

In [ ]:
# define a list 
a = [1,2,3,4,2,2]
print a

In [5]:
# list of numbers from 0 to 9
a = range(10)
a


Out[5]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Accesing elements of a list


In [6]:
# Python is zer-bases indexing
a[0]


Out[6]:
0

In [7]:
# Get the last element
a[-1]


Out[7]:
9

In [8]:
# Get the next to the last element
a[-2]


Out[8]:
8

In [9]:
a[:]


Out[9]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [11]:
# Slice the list
a[0:6] # elements with indecies 0, 1, 2, & 3


Out[11]:
[0, 1, 2, 3, 4, 5]

In [12]:
a = [1,2,2,3,4,4,4,6,7,2,2,2]

In [14]:
# Get the number of occurences of the element 2
a.count(2)


Out[14]:
5

In [15]:
# the original list
a


Out[15]:
[1, 2, 2, 3, 4, 4, 4, 6, 7, 2, 2, 2]

In [16]:
# remove the element at with index 2 and return that value
a.pop(2)


Out[16]:
2

In [17]:
# a is now modified
a


Out[17]:
[1, 2, 3, 4, 4, 4, 6, 7, 2, 2, 2]

In [18]:
# delete without return
del a[1] # delete element at index 1

In [19]:
# print a
a


Out[19]:
[1, 3, 4, 4, 4, 6, 7, 2, 2, 2]

In [20]:
2 not in a


Out[20]:
False

In [21]:
5 in a


Out[21]:
False

In [22]:
# list can contain any type of Python objects, including lists
f = [1, '2', 'a string', [1, ('3', 2)], {'a':1, 'b':2}]

In [23]:
# get element @ index 2
f[2]


Out[23]:
'a string'

In [26]:
# change it
f[2] = 3
f


Out[26]:
[1, '2', 3, [1, ('3', 2)], {'a': 1, 'b': 2}]

In [25]:
# length of the list
len(f)


Out[25]:
5

In [3]:
import random
# list comprehension
a = [int(100*random.random()) for i in xrange(150)]
print a

# the same as
a = []
for i in range(150):
    a.append(int(100*random.random()))
    
    
# get the max and min of a numeric list
max(a), min(a)


[66, 17, 4, 50, 89, 43, 32, 4, 26, 12, 98, 21, 99, 81, 47, 94, 52, 58, 39, 4, 79, 76, 3, 59, 83, 66, 80, 61, 0, 0, 87, 44, 93, 78, 96, 1, 98, 65, 45, 11, 61, 8, 48, 33, 20, 28, 28, 77, 68, 29, 9, 41, 6, 93, 4, 4, 90, 18, 25, 32, 20, 13, 74, 85, 34, 15, 30, 46, 27, 22, 0, 77, 48, 20, 52, 79, 0, 30, 25, 13, 85, 14, 41, 92, 79, 3, 25, 81, 96, 48, 63, 18, 2, 0, 70, 73, 58, 87, 51, 34, 0, 67, 69, 84, 82, 58, 71, 86, 85, 26, 89, 98, 99, 65, 42, 24, 21, 91, 98, 32, 95, 1, 93, 73, 51, 89, 6, 96, 29, 70, 26, 78, 13, 46, 84, 19, 92, 7, 44, 15, 25, 97, 27, 2, 51, 68, 89, 37, 83, 59]
Out[3]:
(99, 0)

In [31]:
# make a tuple into a list
x = (1,2,3,4,5)
list(x)


Out[31]:
[1, 2, 3, 4, 5]

In [35]:
# add object to the end of the list
x = [1,2,3]
x.append(4)
print x
x.append([6,7,8])
print x


[1, 2, 3, 4]
[1, 2, 3, 4, [6, 7, 8]]

In [36]:
# Appends the contents of seq to list
x.extend([9,10,11,12,[13,14,15]])
print x
x.extend([1,2,3])
x


[1, 2, 3, 4, [6, 7, 8], 9, 10, 11, 12, [13, 14, 15]]
Out[36]:
[1, 2, 3, 4, [6, 7, 8], 9, 10, 11, 12, [13, 14, 15], 1, 2, 3]

In [37]:
a = [1,2,3]
b = [4,5,6]
c = a+b
c


Out[37]:
[1, 2, 3, 4, 5, 6]

In [38]:
# Returns count of how many times obj occurs in list
x.count(3)


Out[38]:
2

In [39]:
# Returns the lowest index in list that obj appears
x.index(10)


Out[39]:
6

In [40]:
# Inserts object obj into list at offset index
print x[3]
x.insert(3, ['a','b','c'])
print x


4
[1, 2, 3, ['a', 'b', 'c'], 4, [6, 7, 8], 9, 10, 11, 12, [13, 14, 15], 1, 2, 3]

In [41]:
# Removes and returns last object or obj from list
x.pop()
print x
print x[3]
x.pop(3)
print x


[1, 2, 3, ['a', 'b', 'c'], 4, [6, 7, 8], 9, 10, 11, 12, [13, 14, 15], 1, 2]
['a', 'b', 'c']
[1, 2, 3, 4, [6, 7, 8], 9, 10, 11, 12, [13, 14, 15], 1, 2]

In [44]:
# Removes the first occurrence of obj from list
x = [1,2,2,3,4,5,2,3,4,6,3,4,5,6,2]
x.remove(2)
print x


[1, 2, 3, 4, 5, 2, 3, 4, 6, 3, 4, 5, 6, 2]

In [47]:
# Reverses objects of list in place
x.reverse()
print x


[2, 6, 5, 4, 3, 6, 4, 3, 2, 5, 4, 3, 2, 1]

In [48]:
# Sort x in place
x.sort()
print x


[1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 6, 6]

In [61]:
# duplicate a list
a = [1,2,3]
b = a*5
b


Out[61]:
[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]

In [5]:
import random
[random.random() for _ in range(0, 10)]


Out[5]:
[0.08369225271274194,
 0.9849393026604534,
 0.309750348111128,
 0.05429786386359414,
 0.6032219078216607,
 0.34034619117595544,
 0.1957764842129568,
 0.5200160385516592,
 0.24937990315873793,
 0.7363230245652187]

In [4]:
x = [random.randint(0,1000) for _ in range(10)]
print x


[203, 523, 940, 380, 332, 866, 609, 271, 324, 389]

In [6]:
random.choice(x)


Out[6]:
609

In [7]:
print range(10)
print range(0,10)
print range(5,16)
print range(-6, 7, 2)


[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
[-6, -4, -2, 0, 2, 4, 6]

In [15]:
M=[[1,2,3],
   [4,5,6],
   [7,8,9]]
print M


[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

In [16]:
# put the 2nd column of M in a list
column = []
for row in M:
    column.append(row[1])
print column


[2, 5, 8]

In [17]:
# list comprehension - another way of extracting the 2nd column of M
column = [row[1] for row in M]
print column


[2, 5, 8]

In [18]:
# compute the transpose of the matrix M
[[row[i] for row in M] for i in range(3)]


Out[18]:
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]

In [19]:
# get the diagonal elements of M
diag = [M[i][i] for i in [0, 1, 2]]
print diag


[1, 5, 9]

In [20]:
# build a list with another list as elements
[[x ** 2, x ** 3] for x in range(4)]


Out[20]:
[[0, 0], [1, 1], [4, 8], [9, 27]]

In [21]:
# build a list with an if statement
[[x, x/2, x*2] for x in range(-6, 7, 2) if x > 0]


Out[21]:
[[2, 1, 4], [4, 2, 8], [6, 3, 12]]

In [22]:
# does the same thing as above but more 
big_list = []

for x in range(-6,7,2):
    if x > 0:
        big_list.append([x, x/2, x*2])

print big_list


[[2, 1, 4], [4, 2, 8], [6, 3, 12]]

In [23]:
# does the same as above but lots of code
big_list = []

for x in range(-6,7,2):
    lil_list = []
    if x > 0:
        lil_list.append(x)
        lil_list.append(x/2)
        lil_list.append(x*2)
        big_list.append(lil_list)

print big_list


[[2, 1, 4], [4, 2, 8], [6, 3, 12]]

In [24]:
L = ["Good", # clint
     "Bad",  # 
     "Ugly"]   

print L


['Good', 'Bad', 'Ugly']