[1, 2, 3, 4, 5, 6]
>>>
Again, the result is a single list of data values.
Other List Functions
In addition to the list functions already discussed, Python includes a few other handy list functions by
default. For example, you can count how many times a specific data value appears within a list by
using the count() function, as shown here:
Click here to view code image
>>> list10 = [1, 5, 8, 1, 34, 75, 1, 23, 34, 100]
>>> list10.count(1)
3
>>> list10.count(34)
2
>>>
The 1 value occurs three times in the list, and the 34 value occurs twice in the list.
You can use the sort() function to sort the data values in a list, as in this example:
Click here to view code image
>>> list11 = ['oranges', 'apples', 'pears', 'bananas']
>>> list11.sort()
>>> print(list11)
['apples', 'bananas', 'oranges', 'pears']
>>>
Watch Out!: Sorting in Place
Notice that the sort() function replaces the original order of the data values with the
sorted order in the list itself. This will change the index location of the individual data
values, so be careful when referencing data values in the new list!
You can find the location of a data value within a list by using the index() function. The index()
function returns the index location value of the first occurrence of the data value within the list:
>>> list11.index('bananas')
1
>>>
You can easily reverse the order of the data values stored in a list by using the reverse() function,
like this:
>>> list12 = [1, 2, 3, 4, 5]
>>> list12.reverse()
>>> print(list12)
[5, 4, 3, 2, 1]
>>>
All the data values are still in the list, just in the opposite order from where they started.
Using Multidimensional Lists to Store Data
Python supports the use of multidimensional lists—that is, lists that contain data values that