LISTS AND FUNCTIONS
Appending to a list
Here, we’ll quickly recap how to
.append()
elements to the end of a list.
i.e
n= [11, 16]
# Append the number 27 here
n.append(27)
print(n)
Removing elements from lists
This exercise will expand on ways to remove items from a list. You actually have a few options. For a list called
n
:n.pop(index)
will remove the item atindex
from the list and return it to you:n = [9, 5, 8, 7] n.pop(1) # Returns 5 (the item at index 1) print n # prints [9, 8, 7]n.remove(item)
will remove the actualitem
if it finds it:n.remove(1) # Removes 1 from the list, # NOT the item at index 1 print n # prints [9, 8, 7]del(n[1])
is like.pop
in that it will remove the item at the given index, but it won’t return it:del(n[1]) # Doesn't return anything print n # prints [9, 8, 7]
Printing out a list item by item in a function
This exercise will go over how to utilize every element in a list in a function. You can use the existing code to complete the exercise and see how running this operation inside a function isn’t much different from running this operation outside a function.
Don’t worry about the
range
function quite yet—we’ll explain it later in this section.Passing a range into a function
Okay! Range time. The Python
range()
function is just a shortcut for generating a list, so you can use ranges in all the same places you can use lists.
range(6) # => [0, 1, 2, 3, 4, 5]
range(1, 6) # => [1, 2, 3, 4, 5]
range(1, 6, 3) # => [1, 4]
The
range
function has three different versions:range(stop)
range(start, stop)
range(start, stop, step)
In all cases, the
range()
the function returns a list of numbers from start
up to (but not including) stop
. Each item increases by step
.
If omitted,
start
defaults to 0
and step
defaults to 1
.Iterating over a list in a function
Now that we’ve learned about
range
, we have two ways of iterating through a list.
Method 1 - for item in list
:
for item in list:
print item
Method 2 - iterate through indexes:
for i in range(len(list)):
print list[i]
Method 1 is useful to loop through the list, but it’s not possible to modify the list this way.
Method 2 uses indexes to loop through the list, making it possible to also modify the list if needed. Since we aren’t modifying the list, feel free to use either one on this lesson!
Using strings in lists in functions
Now let’s try working with strings!
for item in list:
print item
for i in range(len(list)):
print list[i]
The example above is just a reminder of the two methods for iterating over a list.
Using two lists as two arguments in a function
Using multiple lists in a function is no different from just using multiple arguments in a function!
a = [11, 16, 27]
b = [1, 5, 10]
print a + b
# prints [11, 16, 27, 1, 5, 10]
The example above is just a reminder of how to concatenate two lists.
Using a list of lists in a function
Finally, this exercise shows how to make use of a single list that contains multiple lists and how to use them in a function.
list_of_lists = [[1, 2, 3], [4, 5, 6]]
for lst in list_of_lists:
for item in lst:
print item
- In the example above, we first create a list containing two items, each of which is a list of numbers.
- Then, we iterate through our outer list.
- For each of the two inner lists (as
lst
), we iterate through the numbers (asitem
) and print them out.
We end up printing out:
1
2
3
4
5
6
No comments: