More Indexing
#Grab every second element starting at index 0 up to but not including index 4
print(l[0:4:2])
print()
#Grab every second element starting at index 1 up to but not including index 4
print(l[1:4:2])
Reverse a List¶
Finally, we can reverse a list by passing in -1 for i3. Below we show the list and the reversed list.
#Print the regular list
print(l)
print()
#Print the reversed list
print(l[::-1])
Finding the Index of an Element¶
Finding the index of an element works in a similar manner to strings except we call the function index on our list. The following finds the index of the string c in our list. It also finds the first instance.
#Index will return the index of the first variable found
l = ['a','b','c']
print(l.index('c'))
One major difference is that if we look for the index of something which is not there, it returns an error in this case. We need to be much more careful when using index.
#This will throw an error because d is not in the list
l = ['a','b','c']
print(l.index('d'))
The same thing applies with this index function as with strings where we are allowed to give an index to begin our search on. For the below piece of code we look for c but only after index 3!
#Search after and including a given index
l = ['a','b','c', 'a','b','c']
print(l.index('c', 3))
A simple example to combine index and pop is shown below where we first find the index of 100, then pop it from the list and set a variable equal to it.
#Example: Find the index of 100, then use that to set a variable a equal to it by using pop
l = [1,2,3,100,2,3]
#Find the index of 100
index = l.index(100)
#Pop that index and set the resulting number as variable a
a = l.pop(index)
#Print the list and variables
print(l)
print(a)
Count Function¶
The count function that lists have returns the number of instances of a given element. Below we have a list with 2 instances of a, 1 instance of b, and 0 instances of 0. We use the count function to find these numbers.
#Count gives us the count of variables
l = ['a','a','b']
print(l.count('a'))
print(l.count('b'))
print(l.count('c'))
Sort Function¶
The sort function for a list sorts it in place (meaning you do not need to re-assign the variable). In the example below it shows how the list is sorted after calling it.
#Sort will sort an array in place
l = [3,5,2,1,4]
print(l)
l.sort()
print(l)
One way to find the maximum of a list is to sort it and grab the last value.
#Example: Use sort to find the max value
l = [3,5,2,1,4]
l.sort()
print(l[-1])
Max and Min Function¶
Calling max() and passing a list in will also give you back the maximum value of a list. The min() function returns the minimum.
#Python also has max() to do this for you easily
print(max(l))
print(min(l))
If you prefer the list to be sorted from largest to smallest, you can give the argument reverse=True.
#The parameter reverse=True will give us the list with the highest numbers first
l.sort(reverse=True)
print(l)
Multiplication for Lists¶
When you multiply a list by an integer n, it will replicate that list n times. The code below will take the list and replicate it twice.