Practice these examples using Repl.it to become more familiar with some methods commonly applied to lists. As you go through these examples, you should begin to see how powerful Python can be as a programming language.
Python has a great built-in list type named "list". List literals are written within square brackets [ ]. Lists work similarly to strings -- use the len() function and square brackets [ ] to access data, with the first element at index 0.
colors = ['red', 'blue', 'green'] print(colors[0]) ## red print(colors[2]) ## green print(len(colors)) ## 3
b = colors ## Does not copy the list
for var in list
--
is an easy way to look at each element in a list (or other collection). Do not add or remove from the list during iteration.
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.squares = [1, 4, 9, 16] sum = 0 for num in squares: sum += num print(sum) ## 30
value in collection
--
tests if the value is in the collection, returning True/False.
The for/in constructs are very commonly used in Python code and work on data types other than list, so you 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.list = ['larry', 'curly', 'moe'] if 'curly' in list: print('yay')
for ch in s: print ch
prints
all the chars in a string.
There is a variant xrange() which avoids the cost of building the whole list for performance-sensitive cases (in Python 3000, range() will have the good performance behavior and you can forget about xrange()).## print the numbers from 0 through 99 for i in range(100): print(i)
## Access every 3rd element in a list i = 0 while i < len(a): print(a[i]) i = i + 3
Common error: note that the above methods do not *return* the modified list, they just modify the original list.list = ['larry', 'curly', 'moe'] list.append('shemp') ## append elem at end list.insert(0, 'xxx') ## insert elem at index 0 list.extend(['yyy', 'zzz']) ## add list of elems at end print(list) ## ['xxx', 'larry', 'curly', 'moe', 'shemp', 'yyy', 'zzz'] print(list.index('curly')) ## 2 list.remove('curly') ## search and remove that element list.pop(1) ## removes and returns 'larry' print(list) ## ['xxx', 'moe', 'shemp', 'yyy', 'zzz']
list = [1, 2, 3] print(list.append(4)) ## NO, does not work, append() returns None ## Correct pattern: list.append(4) print(list) ## [1, 2, 3, 4]
list = [] ## Start as the empty list list.append('a') ## Use append() to add elements list.append('b')
list = ['a', 'b', 'c', 'd'] print(list[1:-1]) ## ['b', 'c'] list[0:2] = 'z' ## replace ['a', 'b'] with ['z'] print(list) ## ['z', 'c', 'd']
Source: Google, https://developers.google.com/edu/python/lists#list-methods
This work is licensed under a Creative Commons Attribution 4.0 License.