Operations on Lists
The last unit showed us how to use a for
loop on a list. Lets revise, how does a for
loop look like?
>>> for i in range(0,9):
print i
This would result in:
0
1
2
3
4
5
6
7
8
A for
loops make our life easier by reducing the number of lines of code. It makes our work more efficient and quick. Let's apply this on a list:
listExample = ['apple', 'berry', 'cherry']
for i in range(len(listExample)): # Using the `len()` function
print listExample[i]
This will print:
apple
berry
cherry
Note: As a
for
loop iterates over a range of items, we use therange()
function over the length of the list. This helps us to iterate our for loop from 0 to the length of the list.
What else can we do with Lists?
Finding the existence of values in Lists
One thought that often comes to the mind is, what if the list we have is huge and we need to check the presence of a particular value? Will we have to run a for
loop over the entire list just for this value? Not exactly! Python has a solution for this as well. The in
and not in
operators. These are used in expressions where the result is Boolean, i.e., True
or False
.
>>> listExample = ['apple', 'berry', 'cherry']
>>> 'apple' in listExample
True
>>> 'blueberry' in listExample
False
>>> 'blueberry' not in listExample
True
Augmented Assignment Statements
This is another cool feature in Python, to reduce your code and increase efficiency. Often, while using assignment operators, we repeat the variable name to add, subtract, divide, get remainder or multiple new values. This can be made easy.
>>> data = 0
>>> data = data + 1
>>> data
1
>>> data += 1
>>> data
2
As you can see, using +=
in the adding operator was a shortcut than writing the entire variable name again. This is a table for all the augmented assignment statements in Python
Augmented Assignment Statement | Regular Assignment statement |
---|---|
data *= 1 | data = data * 1 |
data += 1 | data = data + 1 |
data /= 1 | data = data / 1 |
data %= 1 | data = data % 1 |
data -= 1 | data = data - 1 |
PRO TIP: Adding on to what we read earlier, the
+=
operator can be used for String and List concatenation, while the*=
operator can be used for String and List replication. Try it!
Assignments with Lists
Lets consider an example. We have a List of characteristics of a person, like the name, age, height, weight and we wish to assign it to different variables. One way to do this would be:
>>> student = ['Alex', 29, 179, 150]
>>> name = student[0]
>>> age = student[1]
>>> height = student[2]
>>> weight = student[3]
Or, we could do this:
>>> student = ['Alex', 29, 179, 150]
>>> name, age, height, weight = student
>>> name
Alex
Lists allow us to do multiple assignments, iff the size of the lists and variables is the same. Otherwise, it throws a ValueError
.
>>> student = ['Alex', 29, 179, 150]
>>> name, age, height, weight, sex = student
ValueErrorTraceback (most recent call last)
<ipython-input-16-219fff1c0b23> in <module>()
1 student = ['Alex', 29, 179, 150]
----> 2 name, age, height, weight, sex = student
ValueError: need more than 4 values to unpack
DO IT YOURSELF: Try swapping two values using the multiple assignment method!