Lists Ruth Anderson University of Washington CSE 160 Winter 2016 1.

Slides:



Advertisements
Similar presentations
Lists Ruth Anderson CSE 140 University of Washington 1.
Advertisements

Container Types in Python
Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Lists Introduction to Computing Science and Programming I.
2/28/2008. >>> Overview Arrays in Python – a.k.a. Lists Ranges are Lists Strings vs. Lists Tuples vs. Lists Map-Reduce Lambda Review: Printing to a file.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
Handling Lists F. Duveau 16/12/11 Chapter 9.2. Objectives of the session: Tools: Everything will be done with the Python interpreter in the Terminal Learning.
Hossain Shahriar Announcement and reminder! Tentative date for final exam need to be fixed! Topics to be covered in this lecture(s)
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Built-in Data Structures in Python An Introduction.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 8 Working.
Getting Started with Python: Constructs and Pitfalls Sean Deitz Advanced Programming Seminar September 13, 2013.
10. Python - Lists The list is a most versatile datatype available in Python, which can be written as a list of comma-separated values (items) between.
Lists. The list is a most versatile datatype available in Python which can be written as a list of comma-separated values (items) between square brackets.
Lecture 19 - More on Lists, Slicing Lists, List Functions COMPSCI 101 Principles of Programming.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
Lists CS303E: Elements of Computers and Programming.
1 CSC 221: Introduction to Programming Fall 2011 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
List comprehensions (and other shortcuts) UW CSE 160 Spring 2015.
Collections Michael Ernst CSE 190p University of Washington.
1 CSC 221: Introduction to Programming Fall 2012 Lists  lists as sequences  list operations +, *, len, indexing, slicing, for-in, in  example: dice.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 10 Lists 1.
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
LECTURE 3 Python Basics Part 2. FUNCTIONAL PROGRAMMING TOOLS Last time, we covered function concepts in depth. We also mentioned that Python allows for.
LISTS and TUPLES. Topics Sequences Introduction to Lists List Slicing Finding Items in Lists with the in Operator List Methods and Useful Built-in Functions.
Lists Michael Ernst CSE 140 University of Washington.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Lists/Dictionaries. What we are covering Data structure basics Lists Dictionaries Json.
CSc 110, Autumn 2016 Lecture 15: lists Adapted from slides by Marty Stepp and Stuart Reges.
String and Lists Dr. José M. Reyes Álamo.
More on Lists.
Data Structures: Lists
Ruth Anderson University of Washington CSE 160 Spring 2015
List comprehensions (and other shortcuts) UW CSE 160 Winter 2016.
List comprehensions (and other shortcuts) UW CSE 160 Winter 2017.
Engineering Computing
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Lists in Python.
Bryan Burlingame Halloween 2018
Ruth Anderson University of Washington CSE 160 Spring 2018
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Chapter 10 Lists.
Lecture 15: lists Adapted from slides by Marty Stepp and Stuart Reges
Ruth Anderson University of Washington CSE 160 Winter 2017
8 – Lists and tuples John R. Woodward.
Lecture 22: lists Adapted from slides by Marty Stepp and Stuart Reges
List comprehensions (and other shortcuts) UW CSE 160 Spring 2018.
Lists A list is an ordered set of values, where each value is identified by an index. The values that make up a list are called its elements. Lists are.
String and Lists Dr. José M. Reyes Álamo.
Chapter 5: Lists and Dictionaries
Ruth Anderson UW CSE 160 Winter 2017
Lecture 19: lists Adapted from slides by Marty Stepp and Stuart Reges
Topics Sequences Introduction to Lists List Slicing
Lists Part 1 Taken from notes by Dr. Neil Moore
CSc 110, Spring 2018 Lecture 27: Lists that change size and File Output Adapted from slides by Marty Stepp and Stuart Reges.
15-110: Principles of Computing
Introduction to Computer Science
Module 8 – Searching & Sorting Algorithms
Bryan Burlingame Halloween 2018
Topics Sequences Introduction to Lists List Slicing
Python Review
CSE 231 Lab 6.
CSE 190p University of Washington Michael Ernst
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Lists Like tuples, but mutable. Formed with brackets: Assignment: >>> a = [1,2,3] Or with a constructor function: a = list(some_other_container) Subscription.
COMPUTER SCIENCE PRESENTATION.
Python List.
Ruth Anderson UW CSE 160 Winter 2016
Presentation transcript:

Lists Ruth Anderson University of Washington CSE 160 Winter

What is a list? A list is an ordered sequence of values What operations should a list support efficiently and conveniently? – Creation – Querying – Modification “Four”“score”“and”“seven”“years” 2

List creation a = [ 3, 1, 2*2, 1, 10/2, 10-1 ] b = [ 5, 3, 'hi' ] c = [ 4, 'a', a ] d = [ [1, 2], [3, 4], [5, 6] ]

List Querying Extracting part of the list: – Single element: mylist[index] – Sublist (“slicing”): mylist[startidx : endidx] Find/lookup in a list – elt in mylist Evaluates to a boolean value – mylist.index(x) Return the int index in the list of the first item whose value is x. It is an error if there is no such item. – mylist.count(x) Return the number of times x appears in the list. 4

List Modification Insertion Removal Replacement Rearrangement 5

List Insertion mylist.append(x) – Extend the list by inserting x at the end mylist.extend(L) – Extend the list by appending all the items in the argument list mylist.insert(i, x) – Insert an item before the a given position. – a.insert(0, x) inserts at the front of the list – a.insert(len(a), x) is equivalent to a.append(x) 6

mylist.remove(x) – Remove the first item from the list whose value is x – It is an error if there is no such item mylist.pop([i]) – Remove the item at the given position in the list, and return it. – If no index is specified, a.pop() removes and returns the last item in the list. Notation from the Python Library Reference: The square brackets around the parameter, “[i]”, means the argument is optional. It does not mean you should type square brackets at that position. List Removal 7

List Replacement mylist[index] = newvalue mylist[start:end] = newsublist – Can change the length of the list – mylist[start:end] = [] removes multiple elements – a[len(a):] = L is equivalent to a.extend(L) 8

List Rearrangement list.sort() – Sort the items of the list, in place. – “in place” means by modifying the original list, not by creating a new list. list.reverse() – Reverse the elements of the list, in place. 9

Index expression How to evaluate a list expression There are two new forms of expression: [a, b, c, d]list creation – To evaluate: evaluate each element to a value, from left to right make a list of the values – The elements can be arbitrary values, including lists ["a", 3, 3.14*r*r, fahr_to_cent(-40), [3+4, 5*6]] a[b] list indexing or dereferencing – To evaluate: evaluate the list expression to a value evaluate the index expression to a value if the list value is not a list, execution terminates with an error if the element is not in range (not a valid index), execution terminates with an error the value is the given element of the list value (counting from zero) List expression Same tokens “ [] ” with two distinct meanings 10

List expression examples What does this mean (or is it an error)? ["four", "score", "and", "seven", "years"][2] ["four", "score", "and", "seven", "years"][0,2,3] ["four", "score", "and", "seven", "years"][[0,2,3]] ["four", "score", "and", "seven", "years"][[0,2,3][1]] 11

Exercise: list lookup def index(somelist, value): """Return the position of the first occurrence of the element value in the list somelist. Return None if value does not appear in somelist.""" Examples: gettysburg = ["four", "score", "and", "seven", "years", "ago"] index(gettysburg, "and") => 2 index(gettysburg, "years") => 4 Fact: mylist[index(mylist, x)] == x 12

Exercise: list lookup (Answer) def index(somelist, value): """Return the position of the first occurrence of the element value in the list somelist. Return None if value does not appear in somelist.""" i = 0 for c in somelist: if c == value: return i i = i + 1 return None 13

Exercise: Convert Units ctemps = [-40, 0, 20, 37, 100] # Goal: set ftemps to [-40, 32, 68, 98.6, 212] # Assume a function celsius_to_fahrenheit exists ftemps = [] 14

Exercise: Convert Units (Answer) ctemps = [-40, 0, 20, 37, 100] # Goal: set ftemps to [-40, 32, 68, 98.6, 212] # Assume a function celsius_to_fahrenheit exists ftemps = [] for c in ctemps: f = celsius_to_farenheit(c) ftemps.append(f) 15

List Slicing mylist[startindex:endindex] evaluates to a sublist of the original list – mylist[index] evaluates to an element of the original list Arguments are like those to the range function – mylist[start:end:step] – start index is inclusive, end index is exclusive – All 3 indices are optional Can assign to a slice: mylist[s:e] = yourlist 16

List Slicing Examples test_list = ['e0', 'e1', 'e2', 'e3', 'e4', 'e5', 'e6'] From e2 to the end of the list: test_list[2:] From beginning up to (but not including) e5: test_list[:5] Last element: test_list[-1] Last four elements: test_list[-4:] Everything except last three elements: test_list[:-3] Reverse the list: test_list[::-1] Get a copy of the whole list: test_list[:] 17