a = ['a', 'b', 'c']
b = ['aa', 'bb', 'cc']
c = [1, 'b', 3, 'd']
d = ['ant', -13.8, ['bird', 'dog'] ]
The slicing operation creates a new list, taking elements from the first list that the programmer specifies. You again use the square brackets, but now specify two or three values instead of just one. Separate the values with the colon (:
). The general form for slicing is:
or
The start_index is the index to start slicing at. Stop slicing at the stop_index (will not include this index). The optional step_size indicates how many steps to take before picking the next element; if it is not specified, the default is 1.
Some examples:
You can leave out the start_index or the stop_index (although you still need the colons). If you leave out start_index, it defaults to 0. If you leave out stop_index, it defaults to len(list_name)
.
Some examples:
Slicing makes a new list and puts the elements into that new list. So, if you change the new list, the old list will not change.
Just like with indexing, you can do assignments to a slice. It replaces the specified part of the list with the given value.
You can also replace parts of a list with another list:
Python provides a lot of functions for working with lists (documentation). Below are some common operations.
Operation | Description | Example |
---|---|---|
Adding new elements to a list | ||
list_name.append(value) |
Add an item to the end of the list |
>>> a.append('d') >>> print(a) ['a', 'b', 'c', 'd'] |
list_name.extend(second_list) |
Extend list_name by appending all of the elements from second_list into it |
>>> b = [1, 2, 3] >>> a.extend(b) >>> print(a) ['a', 'b', 'c', 1, 2, 3] |
list_name.insert(index, value) |
Insert an item into the specified index of the list, moving other values down to make room |
>>> a.insert(1, 'b') >>> print(a) ['a', 'b', 'c'] |
list1 + list2 |
Create a new list by concatenating list1 and list2 together |
>>> b = [1, 2, 3] >>> c = a + b >>> print(c) ['a', 'b', 'c', 1, 2, 3] |
Removing elements | ||
del list_name[i] |
Delete element at position i from the list. Shift elements down to fill in the gap. |
>>> del a[2] >>> a ['a', 'b', 'd'] |
del list_name[start_index:stop_index]
del list_name[start_index:stop_index:step_size]
|
Delete the elements selected by the slice. Shift elements down to fill in the gap. |
>>> del a[2:4] >>> a ['a', 'b', 'e'] >>> >>> a = ['a', 'b', 'c', 'd', 'e'] >>> del a[0:len(a):2] >>> a ['b', 'd'] |
list_name.remove(value) |
Delete the first occurrence of value from the list. Shift elements down to fill in the gap. |
>>> a.remove('a') >>> a ['b', 'c', 'a', 'b', 'c'] |
list_name.clear() |
Clear the list. |
>>> a.clear() >>> a [] |
Searching for elements | ||
list_name.index(value)
list_name.index(value, start_index)
list_name.index(value, start_index, stop_index)
|
Search for value in list_name and return the index. If start_index is given, then start at that position. If both start_index and stop_index are specified, search only the sub-list list_name[start_index:stop_index] .
If value is not found, ValueError is raised.
|
>>> a.index('b') 1 >>> a.index('b', 2) 5 >>> a.index('x') Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: 'x' is not in list |
list_name.count(value)
|
Returns the total number of occurrences of value in list_name |
>>> a.count('b') 2 |
value in list_name
|
True if value is in the list, False otherwise |
>>> 'b' in a True >>> 'd' in a False |
value not in list_name
|
False if value is in the list, True otherwise |
>>> 'b' not in a False >>> 'd' not in a True |
Sorting elements | ||
max(list_name)
|
Finds the largest value in list_name. The list must contain values that can be compared! |
>>> max(a) 'd' >>> >>> #numbers and strings aren't comparable >>> a = ['a', 'b', 'c', 1, 2, 3] >>> max(a) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unorderable types: str() > int() |
min(list_name)
|
Finds the smallest value in list_name. The list must contain values that can be compared! |
>>> min(a) 'a' >>> >>> #numbers and strings aren't comparable >>> a = ['a', 'b', 'c', 1, 2, 3] >>> min(a) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unorderable types: str() > int() |
list_name.sort()
|
Puts list_name in sorted order The list must contain values that can be compared! |
>>> a.sort() >>> print(a) ['a', 'b', 'c', 'd', 'e'] |
Lists and Numbers | ||
sum(list_name)
|
Adds up all the numbers in list_name and returns the sum. The list must contain numbers. |
>>> total = sum(a) >>> print(total) 20.8 |
Lists and Strings | ||
string.split()
string.split(delim)
|
Splits the string into a list. If delim is specified, it uses delim as the separator in the string. If delim is not specified, then whitespace (e.g. space, tab, newline) is used. |
>>> a.split() #no delimiter, so use whitespace ['Splits', 'the', 'string', 'into', 'a', 'list.'] >>> >>> a = '12.3, 14.8, 9.2, 12.6' >>> numbers = a.split(',') #split on ',' >>> print(numbers) ['12.3', ' 14.8', ' 9.2', ' 12.6'] >>> #why are there spaces before each number after the first one? >>> >>> numbers = a.split(', ') #split on comma, followed by space >>> print(numbers) ['12.3', '14.8', '9.2', '12.6'] >>> #notice that now each element is a string representation of a number |
sep.join(list_name)
|
Joins a list of strings into a single string with each element in the list separated by sep (a string). |
>>> ' '.join(a) 'Splits the string into a list.' |
How do you think you'd find the second largest value in a list?
When introducing for loops, one example was counting the number of vowels in text (program duplicated below). How could we simplify the code by using a list?
How would you find the index of the largest element in the list?
The index method only finds one occurrence. Write code that finds the index of all occurrences of a value. Store the indices in a list.
Given a list of numbers, add up all of the numbers at even positions. Try writing this in one line of code.
The split
function breaks a string into a list of strings. What if that string were a list of numbers (e.g. '12.3, 14.8, 9.2, 12.6'). How could you convert it into a list of numbers?
The join
function converts a list of strings into one string. How could you use join to convert a list of numbers into a string?
To answer the last two questions in the section above, you need to write a loop (preferably a for loop). Lists and for loops go together so often, the Python language designers created list comprehension to streamline the creation of new lists using for loops.
Using list comprehension, you can transform a for loop like this one:
into this:
In fact, you can simplify this even more:
If you don't actually need the numbers list, you can simplify it to:
You can also use an if statement in list comprehension, allowing you to convert:
into:
List comprehension can be used in creative ways, such as rewriting the program from above, where we counted the number of lowercase vowels in a user-provided string. We're able to convert:
into:
or (note the change of sum to len):
list
FunctionThere are times where you will want to convert something into a list. We haven't seen a lot of data structures yet, but a lot of them can be converted into a list using the list "function" (technically, it's a callable class, but it's most often used as a function). The list function takes any iterable object. Iterable objects are objects that we can do this to:
So far, we've see these iterable objects:
When we use the range
function, we get back a range object. Sometimes we want an actual list of the integers represented by the range. The example below shows how to convert a range object into a list.
Tuples are almost like lists, except they can't change their values. So once a tuple is created, you cannot add new elements to it nor can you change the value of existing elements. To create a tuple:
()
a,
or (a,)
a, b, c
or (a, b, c)
You can also convert an iterable type into a tuple using the tuple
"function":
<< Previous Notes | Daily Schedule | Next Notes >> |