Storing elements in a sequence in Python (Part 2)

Functions that do not change the list

With mutation always in the back of our minds, let's look at some built-in functions, starting with ones that do not change the list.

seq = [3, 6, 7, 2, 1, 4, 7, 5]

print(min(seq))

print(max(seq))

The built-in functions min and max return the minimum and maximum values in a list, respectively. The code gives the output shown here:


1
7
print(min(["b", "a"]))

min applies to strings as well. The code outputs the following:


a
print(min(["a", 1]))

These functions result in an error when there is no ordering of all the elements:


Traceback (most recent call last):
  File "<string>", line 9, in <module>
TypeError: unorderable types: int() < str()
seq = [3, 6, 7, 2, 1, 4, 7, 5]

print(seq.count(7))

print(seq.count(9))

Here, dot notation is used to ask for the number of times the element 7 appears in the list, as well as the number of times the element 9 appears in the list. count returns 0 for a value that does not appear in the list. Thus, the output is as follows:


2
0
seq = [3, 6, 7, 2, 1, 4, 7, 5]

print(seq.index(2))

print(seq.index(9))

Here we are asking for the position of the first appearance of 2, as well as the position of the first appearance of 9. index returns an error message for a value that does not appear in the list. Thus, the output is as follows:


3
Traceback (most recent call last):
  File "<string>", line 9, in <module>
ValueError: 9 is not in list
seq = [3, 6, 7, 2, 1, 4, 7, 5]

print(2 in seq)

The in function produces true when an item is in a list:


True
seq = [3, 6, 7, 2, 1, 4, 7, 5]

other = list(seq)
print(other)
print(other is seq)

Earlier, we saw that assigning a new name to a list resulted in aliasing. To get a new list, we can use the list function. This code give the following output:


[3, 6, 7, 2, 1, 4, 7, 5]
False
seq = [3, 6, 7, 2, 1, 4, 7, 5]

print(sorted(seq))
print(seq)

This last function, sorted, comes with a warning. There are lots of calculations that can be computed by sorting elements, and many of them can be done in a simpler way. Don't let the existence of built-in sorting stifle your creativity. The output generated by this code is as follows:


[1, 2, 3, 4, 5, 6, 7, 7]
[3, 6, 7, 2, 1, 4, 7, 5]
sample = 'bcdafgnrkf'

print(min(sample))
print(max(sample))
print(sample.count("f"))
print(sample.index("d"))
print("g" in sample)

It may not come as a surprise that all these functions also work on strings. Here individual characters play the role of elements. The output of this code is as follows:


a
r
2
2
True
sample = 'bcdafgnrkf'

other = list(sample)
print(other)

print(sorted(sample))
print(sample)

We can even apply sorting, because there is an ordering on characters. Notice that both list and sorted return lists, even when the inputs are strings:


['b', 'c', 'd', 'a', 'f', 'g', 'n', 'r', 'k', 'f']
['a', 'b', 'c', 'd', 'f', 'f', 'g', 'k', 'n', 'r']
bcdafgnrkf

The functions we have examined are summarized here. Notice that there are three types of syntax here: one for min, max, list, and sorted, one for count and index, and one for in.

Functions that do not change the list
Function Python
minimum min(seq)
maximum max(seq)
count seq.count(7)
search seq.index(2)
membership 2 in seq
new list list(seq)
sort sorted(seq)

String functions

The functions in the previous table are also string functions.

list and sorted return lists.

Remember that list and sorted always return lists.


Functions that change the list

Python has many functions that change lists; we'll only consider a small sample.

seq = [3, 6, 7, 2, 1, 4, 2, 5]

print(seq.append(20))

print(seq)

Remember how you need to be careful to concatenate lists, not a list and an item? You can use append on an item. The None indicates that no value was returned by the function. We need to print our list to see how the original list was changed. The code generates the following output:


None
[3, 6, 7, 2, 1, 4, 2, 5, 20]
seq = [3, 6, 7, 2, 1, 4, 2, 5]

seq.append([20])
print(seq)

Be careful: if you append a list, one of the items will be a list. We'll talk more about lists of lists later. This code generates the following output:


[3, 6, 7, 2, 1, 4, 2, 5, [20]]
seq.remove(6)
print(seq)

We can remove an item by inputting the value in the remove function. Appending the previous two code blocks will generate the output:


[3, 6, 7, 2, 1, 4, 2, 5, [20]]
[3, 7, 2, 1, 4, 2, 5, [20]]
seq.remove(2)
print(seq)

If the value given to remove appears more than once, just the first one will be removed. Appending the previous three code blocks generates the output:


[3, 6, 7, 2, 1, 4, 2, 5, [20]]
[3, 7, 2, 1, 4, 2, 5, [20]]
[3, 7, 1, 4, 2, 5, [20]]
seq.insert(2, 100)
print(seq)

To insert an element, we give the function insert its index and value. Appending the previous four code blocks generates the output:


[3, 6, 7, 2, 1, 4, 2, 5, [20]]
[3, 7, 2, 1, 4, 2, 5, [20]]
[3, 7, 1, 4, 2, 5, [20]]
[3, 7, 100, 1, 4, 2, 5, [20]]
seq.sort()
print(seq)

We've already seen how to create a new list with the values in sorted order; sort changes the original list. Appending the previous five code blocks generates the output:


[3, 6, 7, 2, 1, 4, 2, 5, [20]]
[3, 7, 2, 1, 4, 2, 5, [20]]
[3, 7, 1, 4, 2, 5, [20]]
[3, 7, 100, 1, 4, 2, 5, [20]]
Traceback (most recent call last):
  File "<string>", line 15, in <module>
TypeError: unorderable types: list() < int()

Why did we get an error message? One of our elements is a list, which can't be compared to the other elements. Let's remove it.

seq = [3, 6, 7, 2, 1, 4, 2, 5]

seq.sort()
print(seq)

And now we can sort our list. We get the following output:


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

Here is a summary of the functions.

Functions that change the list
Function Python
append seq.append(20)
remove seq.remove(6)
insert seq.insert(2, 100)
sort seq.sort()

As all of these functions rely on mutation, they do not work on strings.

Caution

The functions in the previous table are not string functions.

Strings are immutable.

Remember to be careful using both concatenation and append.

Caution

Concatenation joins two lists.

Append joins a list and an item.

And don't forget that there are two sorting functions; make sure you are using the one you want.

Caution

Be careful in choosing how to sort a list.