1 / 27

6. Lists

142-253 Computer Programming. 6. Lists. BSc in Digital Media, PSUIC Aj. Andrew Davison, CoE, PSU Hat Yai Campus E-mail: ad@fivedots.coe.psu.ac.th. How to store data in lists. A look at tuples and dictionaries. What are mutable and immutable data types?. 1. Storing Lots of Data Together.

clydeb
Download Presentation

6. Lists

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 142-253 Computer Programming 6. Lists BSc in Digital Media, PSUIC Aj. Andrew Davison, CoE, PSU Hat Yai Campus E-mail: ad@fivedots.coe.psu.ac.th How to store data in lists. A look at tuples and dictionaries. What are mutable and immutable data types?

  2. 1. Storing Lots of Data Together • Up to now we've stored 1piece of data in 1 variable: • x = 3 • With bigger programs, it's useful if we can store lots of data inside 1 variable • we do this using a Python data structure • I'll look at 3 data structures: • lists, tuples, dictionaries

  3. 2. The List • A list: • family = ["Mum", "Dad", "Brother", "Sister", "Baby"] • This stores 5 pieces of data in a list called family. "Mum" "Dad" "Brother" "Sister" "Baby" family 0 1 4 2 3 • The boxes in a list are numbered • the numbers are called indexes (or indicies) • New boxes can be added; boxes can be deleted.

  4. Adding Things to a List An empty list A list can store anything "hans solo" "like" 3.141592 2.71828 friends 0 1 2 3

  5. Printing Parts of a List The ":" is called a slice. "hans solo" "like" 3.141592 2.71828 friends 0 1 2 3

  6. Using Slice to Copy a List friends "hans solo" "like" 3.141592 2.71828 0 1 2 3 copy a slice numberFriends 3.141592 2.71828 0 1

  7. Modifying Box Data

  8. Other Ways to Add to a List • append() adds 1 item to the end of the list • seen already • extend() adds many items to the end of the list

  9. insert() adds one item somewhere in the list • uses an index number • other boxes move to the right to make room letters "a" "b" "z" "d" "e" "f" "g" "h" 0 1 2 3 4 5 6 7 letters "a" "b" "y" "z" "d" "e" "f" "g" "h" 0 1 2 3 4 5 6 7 8

  10. Deleting from a List • Use: remove(), del(), or pop() letters "a" "b" "y" "z" "d" "e" "f" "g" "h" 0 1 2 3 4 5 6 7 8 letters "a" "b" "y" "d" "e" "f" "g" "h" 0 1 2 3 4 5 6 7 The boxes move left to fill the empty space.

  11. letters "a" "b" "y" "d" "e" "f" "g" "h" 0 1 2 3 4 5 6 7 letters "a" "b" "d" "e" "f" "g" "h" 0 1 2 3 4 5 6 The boxes move left to fill the empty space.

  12. letters "a" "b" "d" "e" "f" "g" "h" 0 1 2 3 4 5 6 letters "a" "b" "d" "f" "g" "h" 0 1 2 3 4 5 "e" x letters "a" "b" "d" "f" "g" 0 1 2 3 4 "h" x

  13. Searching a List Useful as a test in if's and while's

  14. Lists and Loops • The counting loop can use a list:

  15. Sorting Lists The words list is changed.

  16. The words list is not changed. A new list is created.

  17. 3. Tuple: the Unchangeable List Use (...) not [...] Changes not allowed. A tuple is immutable.

  18. This is allowed since a new tuple is being created. tup is not changed.

  19. 4. Using Lists to Build a Grid

  20. list A Grid is a List of Lists list 1 list 2 list 3 : classMarks Read across 97 0 90 92 87 1 92 79 85 88 2 a row 6 13 7 -1 0 1 2 3 a column

  21. Accessing a Grid • Use row and column position rows columns

  22. Accessing a Grid (2D Array) row classMarks 97 0 90 92 87 1 92 79 85 88 2 6 13 7 -1 0 1 2 3 row column

  23. 5. Dictionaries • A dictionary is a list where the index numbers are replaced by strings • the strings are called keys; data are called values "444-4321" "Bob" "John" "555-1234" keys "555-6789" "Mary" values "Jenny" "867-5309"

  24. Building a Dictionary "444-4321" "Bob" "John" "555-1234" "555-6789" "Mary" "Jenny" "867-5309" A dictionary can store key-value pairsin any order it wants.

  25. Accessing all the Keys / Values Use the keys list inside a for loop Use a sorted keys list to get values in key order

  26. Deletion and Checking Big "J" != "j" Can only check for keys

  27. Common Dictionary Operations

More Related