Lists and Functions
Now that you've learned about lists, let's turbo-charge them with functions.
StartLists and Functions
Lesson 1 of 2
- 1This exercise goes over just pulling information from a list, which we’ve covered in a previous section!
- 2You’ve already learned how to modify elements of a list in a previous section. This exercise is just a recap of that!
- 3Here, we’ll quickly recap how to .append() elements to the end of a list.
- 4This exercise will expand on ways to remove items from a list. You actually have a few options. For a list called n: 1. n.pop(index) will remove the item at index from the list and return it to y…
- 5In this exercise, you will just be making a minor change to a function to change what that function does.
- 6This exercise will recap how to use more than one argument in a function.
- 7This is a basic recap on using strings in functions.
- 8You pass a list to a function the same way you pass any other argument to a function.
- 9Passing a list to a function will store it in the argument (just like with a string or a number!) def first_item(items): print items[0] numbers = [2, 7, 9] first_item(numbers) 1. In the examp…
- 10Modifying an element in a list in a function is the same as if you were just modifying an element of a list outside a function. def double_first(n): n[0] = n[0] * 2 numbers = [1, 2, 3, 4] doubl…
- 11You can also append or delete items of a list inside a function just as if you were manipulating the list outside a function. my_list = [1, 2, 3] my_list.append(4) print my_list # prints [1, 2, 3,…
- 12This 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 mu…
- 13This exercise shows how to modify each element in a list. It is useful to do so in a function as you can easily put in a list of any length and get the same functionality. As you can see, len(n) is…
- 14Okay! 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)…
- 15Now 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 …
- 16Now 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.
- 17Using multiple lists in a function is no different from just using multiple arguments in a function! a = [1, 2, 3] b = [4, 5, 6] print a + b # prints [1, 2, 3, 4, 5, 6] The example above is just…
- 18Finally, 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: fo…
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory