In [ ]:
colours = ['red', 'blue', 'green']
print colours[0]
print colours[2]
print len(colours)
Assignment with an = on lists does not make a copy. Instead, assignment makes the two variables point to the one list in memory.
In [ ]:
b = colours ## does not copy list
The "empty list" is just an empty pair of brackets [ ]. The '+' works to append two lists, so [1, 2] + [3, 4] yields [1, 2, 3, 4] (this is just like + with strings).
In [ ]:
squares = [1, 4, 9, 16]
sum = 0
for num in squares:
sum += num
print sum
If you know what sort of thing is in the list, use a variable name in the loop that captures that information such as "num", or "name", or "url". Since python code does not have other syntax to remind you of types, your variable names are a key way for you to keep straight what is going on.
The in construct on its own is an easy way to test if an element appears in a list (or other collection) -- value in collection -- tests if the value is in the collection, returning True/False.
In [ ]:
list = ['larry', 'curly', 'moe']
if 'curly' in list:
print 'yay'
The for/in constructs are very commonly used in Python code and work on data types other than list, so should just memorize their syntax. You may have habits from other languages where you start manually iterating over a collection, where in Python you should just use for/in.
You can also use for/in to work on a string. The string acts like a list of its chars, so for ch in s: print ch prints all the chars in a string.
In [ ]:
for i in range(100):
print i,
There is a variant xrange() which avoids the cost of building the whole list for performance sensitive cases (in Python 3, range() will have the good performance behavior and you can forget about xrange() ).
Python also has the standard while-loop, and the break and continue statements work as in C++ and Java, altering the course of the innermost loop. The above for/in loops solves the common case of iterating over every element in a list, but the while loop gives you total control over the index numbers. Here's a while loop which accesses every 3rd element in a list:
In [ ]:
a = ['a', 34, 3.14, [1,2], 'c']
i = 0
while i < len(a):
print a[i]
i = i + 3
Here are some other common list methods.
Notice that these are methods on a list object, while len() is a function that takes the list (or string or whatever) as an argument.
In [ ]:
list = ['larry', 'curly', 'moe']
In [ ]:
list.append('shemp')
list
In [ ]:
list.insert(0, 'xxx')
list
In [ ]:
list.extend(['yyy', 'zzz'])
list
In [ ]:
print list.index('curly')
In [ ]:
list.remove('curly')
list
In [ ]:
print(list.pop(1))
list
Common error: note that the above methods do not return the modified list, they just modify the original list.
In [ ]:
list = [1, 2, 3]
print(list.append(4))
So list.append() doesn't return a value. 'None' is a python value that means there is no value (roll with it). It's great for situations where in other languages you'd set variables to -1 or something.
One common pattern is to start a list a the empty list [], then use append() or extend() to add elements to it:
In [ ]:
list = []
list.append('a')
list.append('b')
list
Slices work on lists just as with strings, and can also be used to change sub-parts of the list.
In [ ]:
list = ['a', 'b', 'c', 'd']
list[1:-1]
In [ ]:
list[0:2] = 'z'
list
For practice with lists, go to the notebook 3.5 - List exercises
In [ ]:
In [ ]:
In [ ]:
Note: This notebook is based on Google's python tutorial https://developers.google.com/edu/python
In [ ]: