INSY 3300 - Chapter 7 Lists & Tuples

¡Supera tus tareas y exámenes ahora con Quizwiz!

List Methods and Useful Built-in functions (Slide 14) 1/3

- append(item): adds item to the end of the list - index(item): used to determine where an item is located in a list - returns the index of the first element in the list containing item - raises ValueError exception if item not in the list ex: list1=[20,40,50] print (list1.index(40)) will display 1

List Methods and Useful Built-in functions (Slide 14) 3/3

- del statement: removes an element from a specific index in a list. format: del list[I] - min and max functions: built-in functions that return the item that has the lowest or highest value in a sequence. the sequence is passed as an argument (skip to slide 23)

List Methods and Useful Built-in functions (Slide 14) 2/3

- insert(index, item): used to insert item at position index in the list - sort(): used to sort the elements of the list in ascending order - remove(item): removes the first occurrence of item in the list a valueError exception is raised if item is not found in the list - reverse(): reverses the order of the elements in the list

Processing Lists (Slide 20)

List elements can be used in calculations To calculate total of numeric values in a list use loop with accumulator variable to avg numeric values in a list - calculate total of the values - divide total of the values by len(list) list can be passed as an argument to a function to save contents of a list to a file: - use the file object's writelines method does not automatically write \n at the end of each item - use a for loop to write each element and \n to read data from a file use the file object's readlines method

Lists are Mutable (Slide 10)

Mutable Sequence: the items in the sequence can be changed - lists are mutable and so their elements can be changed list[1] = new_value can be used to assign a new value to a list element - must use valid index to prevent raising of an IndexError exception

List Comprehensions (Slide 22-23)

a concise expression that creates a new list by iterating over the elements of an existing list code uses a for loop to make a copy of a list: list1= [1, 2, 3, 4] list2=[] for item in list1: list2.append(item) print(item, list2) - the following code uses a list comprehension to make a copy of a list list1= [1, 2, 3, 4] list2= [item for item in list1] print(list2)

Two-Dimensional Lists (Slide 29)

a list that contains other lists as its elements - known as nested list - common to think of 2-D lists as having rows and columns - useful for working with multiple sets of data - to process data in a 2-D list need to use two indexes - typically use nested loops to process

Indexing

a number specifying the position of an element in a list (similar to array indexing) - enables access to individual element in list - index of first element in the list is 0, second element is 1, and n'th element is n-1 Negative Indexes identify positions relative to the end of the list - the index -1 identifies the last element, -2 identifies the next to last element, etc.

List Slicing (Slide 12)**

a span of items that are taken from a sequence - list slicing format: list[start : end] - span is a list containing copies of elements from start up to, but not including end * if start not specified, 0 is used for start index * if end not specified, len(list) is used for end index - slicing expressions can include a step value and negative index relative to end of list

The len function

an IndexError exception is raised if an invalid index is used len() function: returns the length of a sequence such as a list and a string - ex: size = len(my_list) - returns the number of elements in the list, so the index of last element is len(list)-1 - Can be used to prevent an IndexError exception when iterating over a list with a loop

Tuples (Slide 32-35)

an immutable sequence - very similar to a list format: tuple_name = (item1, item) alternate format: tuple_name=item1, item2, etc (W/O PARENTHESES) - iterate with a for loop -------------------------- Slide 33 Tuples (and Strings) support many operations as Lists - the *, +, and in operators - Indexing for retrieving elements - Slicing expressions (ex: my_tuple[1:4]) - Built-in functions such as len, min, max - Methods such as Index ----------------------------- Slide 34 Once a tuple is created, it cannot be changed Tuples do not support the following methods bc they are immutable: - append, remove, insert, reverse, and sort -------------------------------- Slide 35 list() function: converts tuple to list tuple() function: converts list to tuple Advantages for using tuples over lists: - processing tuples is faster than processing lists - tuples are safe (cannot be modified even accidentally)

List

an object that contains multiple data items - element: an item in a list - format: list = [item1, item2, etc] - can hold items of different types print() function can be used to display an entire list list() function can convert certain types of objects to lists ppt ex: (list of integers) even_numbers = 2, 4, 6, 8, 10 (list of strings) names = Molly, Steven, Will, Alicia, Adriana

Sequences

an object that contains multiple items of data - the items are stored in sequence one after another python provides diff types of sequences including lists and tuples - diff between these is that a list is mutable and a tuple is immutable - Mutable means List contents can be changed - Immutable means Tuple once created, its contents cannot be changed

Finding items in lists with the in operator (Slide 13)

can use the in operator to determine whether an item is contained in a list - general format: item in list - returns True if the item is in the list, or False if it is not - similarly you can use the not in operator to determine whether an item is not in a list

Concatenating Lists (Slide 11)

join two things together - the + operator can be used to concatenate two lists CANNOT concatenate a list with another data type such as a number - the += augmented assignment operator can also be used to concatenate lists

List Comprehensions (Slide 24-28)

list2=[item = result expression, for item in list 1 = iteration expression - iteration expression works like a for loop - example above iterates over the elements of list1 - each time it iterates the target variable item is assigned the value of an element - at the end of each iteration, the value of the result expression is appended to the new list --------------------------------- list1= [1, 2, 3, 4] list2= [item**2 for item in list1] * ^ After this code executes, list2 will contain the values [1, 4, 9, 16] ---------------------------------- slide 26 str_list= ['Winken', 'Blinken', 'Nod'] len_list= [len(s) for s in str_list] * After this code executes, len_list will contain values [6,7,3] ---------------------------------- slide 27/28 you can use an if clause in a list comprehension to select only certain elements when processing a list list1=[1, 12, 2, 20, 3, 15, 4] list2= [item for item in list1 if item <10] *^ After this code executes, list2 will contain [1 , 2, 3, 4]

Repetition Operator and Iterating over a List

makes multiple copies of a list and joins them together - the * symbol is a repetition operator when applied to a sequence and an integer - format: list * n you can iterate over a list using a for loop - format: for x in list:

Copying Lists (Slide 18)

to make a copy of a list you must copy each element of the list 2 methods: 1. creating a new empty list and using a for loop to add a copy of each element from the OG list to the new list 2. creating a new empty list and concatenating the old list to the new empty list


Conjuntos de estudio relacionados

MS-102 Microsoft 365 Administrator Certification

View Set

CPSC 471 Chapter 2 Exam Study Set

View Set

DATA 630: Applied Database Management Week 1-5 Quizes

View Set

26 Quiz 8 - The Nursing Process - Implementing

View Set

Which statement best describes a project?

View Set

Vocabulary Workshop Level H (Units 10-12)

View Set