Python Lists

Ace your homework & exams now with Quizwiz!

When we want to add multiple items to a list, we can use _____ to combine two lists. Below, we have a list of items sold at a bakery called items_sold: We can only use (1) with other _____ If we want to add a single element using +, we have to put it into a list with ______:

+ items_sold = ['cake', 'cookie', 'bread'] >>> items_sold_new = items_sold + ['biscuit', 'tart'] >>> print(items_sold_new) ['cake', 'cookie', 'bread', 'biscuit', 'tart'] lists my_list = [1, 2, 3] my_list + 4 TypeError: can only concatenate list (not "int") to list brackets ([]) my_list + [4]

What if we want to select the last element of a list? We can use the index____- to select the last item of a list, even when we don't know how many elements are in a list.

-1 list1 = ['a', 'b', 'c', 'd', 'e'] >>> print(list1[-1]) 'e' This is the same as selecting the element with index 4: >>> print(list1[4]) 'e'

Sometimes, we want to sort a list in either numerical (1, 2, 3, ...) or alphabetical (a, b, c, ...) order. We can sort a list in place using______. Suppose that we have a list of names:

.sort() names = ['Xander', 'Buffy', 'Angel', 'Willow', 'Giles'] print(names) ['Xander', 'Buffy', 'Angel', 'Willow', 'Giles'] names.sort() print(names) ['Angel', 'Buffy', 'Giles', 'Willow', 'Xander']

Often, we want to create a list of consecutive numbers. For example, suppose we want a list containing the numbers 0 through 9: Typing out all of those numbers takes time and the more numbers we type, the more likely it is that we have a typo. Python gives us an easy way of creating these lists using a function called_______. The function(1) takes a single input, and generates numbers starting at 0 and __________. So, if we want the numbers from 0 through 9, we use ________:

1 range 2 ending at the number before the input 3 range(10) because 10 is 1 greater than 9 my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] my_range = range(10)

We can add a single element to a list using _______ For example, suppose we have an empty list called empty_list: When we use .(1) on a list that already has elements, our new element is added to the______: It's important to remember that (1) comes ______. This is different from functions like ________.

1 .append(). empty_list = [] We can add the element 1 using the following commands: empty_list.append(1) >>> print(empty_list) [1] 2 end of the list 3 after the list 4 print, which come before

A list doesn't have to ________! You can create an _____ list like this: Why would we create an (2) list? Usually, it's because we're planning on filling it _________. We'll talk about two ways of filling up a list in the next exercise.

1 contain anything 2 empty empty_list = [] 3 later based on some other input

Suppose that we already had a list of names and a list of heights: names = ['Jenny', 'Alexus', 'Sam', 'Grace'] heights = [61, 70, 67, 65] If we wanted to create a list of lists that ________, we could use the command zip. zip takes_________. Each pair contains one element from each of the inputs. You won't be able to see much about this object from just ______: To see the nested lists, you can convert the zip object to a _____ first:

1 paired each name with a height 2 two (or more) lists as inputs and returns an object that contains a list of pairs names_and_heights = zip(names, heights) print(names_and_heights) 3 printing it 4 list print(list(names_and_heights)) this returns [('Jenny', 61), ('Alexus', 70), ('Sam', 67), ('Grace', 65)]

Just like with zip, the range function returns an object that we can convert into a list:

>>> print(my_range) range(0, 10) >>> print(list(my_range)) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

We can use range to generate more interesting lists. By default, range creates a list starting at 0. However, if we ___________, we can create a list that starts at a different number.

call range with two arguments For example, range(2, 9) would generate numbers starting at 2 and ending at 8 (just before 9): >>> my_list = range(2, 9) >>> print(list(my_list)) [2, 3, 4, 5, 6, 7, 8]

With one or two arguments, range will create a list of________. If we use a third argument, we can create a list that ________.

consecutive numbers (i.e., each number is one greater than the previous number) "skips" numbers For example, range(2, 9, 2) will give us a list where each number is 2 greater than the previous number: >>> my_range2 = range(2, 9, 2) >>> print(list(my_range2)) [2, 4, 6, 8] >>> my_range3 = range(1, 100, 10) >>> print(list(my_range3)) [1, 11, 21, 31, 41, 51, 61, 71, 81, 91]

Suppose we have a list called letters that represents the letters in the word "Mississippi": letters = ['m', 'i', 's', 's', 'i', 's', 's', 'i', 'p', 'p', 'i'] If we want to know how many times i appears in this word, we can use the function _____:

count letters = ['m', 'i', 's', 's', 'i', 's', 's', 'i', 'p', 'p', 'i'] num_i = letters.count('i') print(num_i) 4

In Python, we call the order of an element in a list its_______. Python lists are_________. This means that the first element in a list has index 0, rather than 1. We can select a single element from a list by using __________. For example, if we wanted to select the third element from the list, we'd use ______:

index zero-indexed square brackets ([]) and the index of the list item calls[2] calls = ['Ali', 'Bob', 'Cam', 'Doug', 'Ellie'] >>> print(calls[2]) 'Cam'

Often, we'll need to find the number of items in a list, usually called its _______. We can do this using the function _____. When we apply(2) to a list, we get the number of elements in that list:

length len my_list = [1, 2, 3, 4, 5] print(len(my_list)) 5

Suppose we have a list of letters: letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] Suppose we want to select from b through f. We can do this using the following syntax: _______, where: (1) is the index of the _____. In this case, we want to start at b, which has index 1. end is the index of one more than the last index that we want to include. The last element we want is f, which has index 5, so end needs to be 6.This example would yield:Notice that the element at index 6 (which is g) is not included in our selection.

letters[start:end] first element that we want to include in our selection sublist = letters[1:6] print(sublist) ['b', 'c', 'd', 'e', 'f']

We can put several of these lists into _______, such that each entry in the list represents a student and their height: What does this look like ?

one list jenny = ['Jenny', 61] heights = [['Jenny', 61], ['Alexus', 70], ['Sam', 67], ['Grace', 64]]

A list is an _______ in Python. Suppose we want to make a list of the heights of students in a class: A list begins and ends with ______ Each item (i.e., 67 or 70) is separated by a _____ It's considered good practice to insert a space () after each comma, but your code will run just fine if you forget the space.

ordered set of objects square brackets ([ and ]). comma (,) Suppose we want to make a list of the heights of students in a class: Jenny is 61 inches tall Alexus is 70 inches tall Sam is 67 inches tall Grace is 64 inches tall heights = [61, 70, 67, 64]

Notice that sort goes after _____, names. If we try sort(names), we will get a _____. sort does not _______. So, if we try to assign names.sort() to a variable, our new variable would be _____:

our list NameError return anything None

A second way of sorting a list is to use ____-. sorted is different from (1) in several ways: It _______

sorted comes before a list, instead of after. It generates a new list. names = ['Xander', 'Buffy', 'Angel', 'Willow', 'Giles'] sorted_names = sorted(names) print(sorted_names) ['Angel', 'Buffy', 'Giles', 'Willow', 'Xander']

Lists can contain both _____ & ________

strings & numbers Lists can contain more than just numbers. Let's revisit our height example: Jenny is 61 inches tall Alexus is 70 inches tall Sam is 67 inches tall Grace is 64 inches tall names = ['Jenny', 'Alexus', 'Sam', 'Grace']


Related study sets

PROOF FORMATS: THE PLAN OF THE PROOF

View Set

Psychology 101 - Chapter 7 - Memory

View Set