Prévia do material em texto
7 for line in poem_lines: 8 print(line) The above code's output is: one fish two fish red fish blue fish CONCEPTS IN PRACTICE List comprehensions 1. The component of a list comprehension defining an element of the new list is the _____. a. expression b. loop_variable c. container 2. What would be the contents of b_list after executing the code below? a_list = [1, 2, 3, 4, 5] b_list = [i+2 for i in a_list] a. [1, 2, 3, 4, 5] b. [0, 1, 2, 3, 4] c. [3, 4, 5, 6, 7] 3. What does new_list contain after executing the statement below? new_list = [i//3 for i in range(1, 15, 3)] a. [0.3333333333333333, 1.3333333333333333, 2.3333333333333335, 3.3333333333333335, 4.333333333333333] b. [0, 1, 2, 3, 4] c. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] Filtering using list comprehensions List comprehensions can be used to filter items from a given list. A condition is added to the list comprehension. list_name = [expression for loop_variable in container if condition] In a filter list comprehension, an element is added into list_name only if the condition is met. 236 9 • Lists Access for free at openstax.org CHECKPOINT Filtering a list Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/9-5-list- comprehensions) CONCEPTS IN PRACTICE Filtering using list comprehensions For each code using list comprehension, select the correct resultant list in new_list. 4. my_list = [21, -1, 50, -9, 300, -50, 2] new_list = [m for m in my_list if m < 0] a. [21, 50, 300, 2] b. [21, -1, 50, -9, 300, -50, 2] c. [-1, -9, -50] 5. my_string = "This is a home." new_list = [i for i in my_string if i in 'aeiou'] a. [i, i, a, o, e] b. ['i', 'i'', 'a', 'o', 'e'] c. Error 6. new_list = [r for r in range (0, 21, 2) if r%2 != 0] a. [] b. [21] c. [1, 3, 5, 7, 9, 11, 13, 15, 17, 19] TRY IT Selecting five-letter words Write a program that creates a list of only five-letter words from the given list and prints the new list. Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/9-5-list- comprehensions) 9.5 • List comprehensions 237 https://openstax.org/books/introduction-python-programming/pages/9-5-list-comprehensions https://openstax.org/books/introduction-python-programming/pages/9-5-list-comprehensions https://openstax.org/books/introduction-python-programming/pages/9-5-list-comprehensions https://openstax.org/books/introduction-python-programming/pages/9-5-list-comprehensions TRY IT Books starting with "A" Write a program that selects words that begin with an "A" in the given list. Make sure the new list is then sorted in dictionary order. Finally, print the new sorted list. Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/9-5-list- comprehensions) 9.6 Chapter summary Highlights from this chapter include: • Lists are mutable and can be easily modified by using append(), remove(), and pop() operations. • Lists are iterable and can be iterated using an iterator or element indexes. • The sort() operation arranges the elements of a list in ascending order if all elements of the list are of the same type. • The reverse() operation reverses a list. • The copy() method is used to create a copy of a list. • Lists have built-in functions for finding the maximum, minimum, and summation of a list for lists with only numeric values. • Lists can be nested to represent multidimensional data. • A list comprehension is a compact way of creating a new list, which can be used to filter items from an existing list. At this point, you should be able to write programs using lists. Function Description append(element) Adds the specified element to the end of a list. remove(element) Removes the specified element from the list if the element exists. pop() Removes the last element of a list. max(list) Returns the maximum element of the list specified. min(list) Returns the maximum element of the list specified. sum(list) Returns the summation of a list composed of numbers. sort() Sorts a list on which the method is called in ascending order. Table 9.1 Chapter 9 reference. 238 9 • Lists Access for free at openstax.org https://openstax.org/books/introduction-python-programming/pages/9-5-list-comprehensions https://openstax.org/books/introduction-python-programming/pages/9-5-list-comprehensions Function Description reverse() Reverses the order of elements in a list. copy() Makes a complete copy of a list. Table 9.1 Chapter 9 reference. 9.6 • Chapter summary 239 240 9 • Lists Access for free at openstax.org Chapter 9 Lists 9.6 Chapter summary