Lists and dictionaries Presenter: Steve Baskauf - - PowerPoint PPT Presentation

lists and dictionaries
SMART_READER_LITE
LIVE PREVIEW

Lists and dictionaries Presenter: Steve Baskauf - - PowerPoint PPT Presentation

Lists and dictionaries Presenter: Steve Baskauf steve.baskauf@vanderbilt.edu CodeGraf landing page vanderbi.lt/codegraf List objects List objects A list object is a one-dimensional data structure Lists can hold any other kind of


slide-1
SLIDE 1

Lists and dictionaries

Presenter: Steve Baskauf steve.baskauf@vanderbilt.edu

slide-2
SLIDE 2

CodeGraf landing page

  • vanderbi.lt/codegraf
slide-3
SLIDE 3

List objects

slide-4
SLIDE 4

List objects

'apple' 'orange' 'banana' 'lemon' 'lime' basket[2] basket[0] refers to the value in that position

  • A list object is a one-dimensional data structure
  • Lists can hold any other kind of object.
  • The items in a list are referred to by an index number (0-based)

basket

slide-5
SLIDE 5

Instantiating a list

'apple' 'orange' 'banana' 'lemon' 'lime' basket = ['apple', 'orange', 'banana', 'lemon', 'lime'] basket[2] basket[0]

  • A list can be constructed directly by listing its contents.
  • The type of the list is different from the type of items the list contains.
  • List items don't have to all be of the same type (but often are).
slide-6
SLIDE 6

Finding the length of a list

  • The len() function will return the number of items in a list
  • Example:

basket = ['apple', 'orange', 'banana', 'lemon', 'lime'] print(len(basket))

  • Item indices range from 0 to 4
  • Length is 5 (the actual count)
  • In many ways, a string is like a list of characters; len() works for it
slide-7
SLIDE 7

Other ways to make a list

slide-8
SLIDE 8

Output of functions or methods

  • The output of a function or method may be a list:
  • os.listdir() function
  • random.sample() function
  • .split() string method
slide-9
SLIDE 9

Slicing a list

'apple' 'orange' 'banana' 'lemon' 'lime' basket[1:4] creates a list containing values in that range

  • A range is given instead of a single index
  • Start of range is zero-based.
  • End of range is one less than ending index.
  • Slicing generates another list
slide-10
SLIDE 10

Aside: slicing a string

  • Since a string is like a list of characters, we can slice it in the same way
  • Example:

a_word = 'Mississippi' word_piece = a_word[1:4]

  • Range is from 1 to 4
  • Slice goes from letters 1 to 3 (start counting with 0)
  • Answer: 'iss'
slide-11
SLIDE 11

Useful things to do with lists

  • Randomize a list
  • random.shuffle() function
  • Sort a list
  • .sort() list method
  • Pick an item from a list
  • random.choice() function
slide-12
SLIDE 12

Changing a list

slide-13
SLIDE 13

Editing lists

'apple' 'tangerine' 'banana' 'lemon' 'lime' basket = ['apple', 'orange', 'banana', 'lemon', 'lime'] basket.append('durian') 'durian' basket[1] = 'tangerine' The .append() method does not return a value – it changes the list. We can assign a new value to any list item.

slide-14
SLIDE 14

More commands for editing lists

  • An empty list can be created using

basket = []

  • .remove() can be used to remove a particular value from the list.
  • del basket[3] can be used to remove an item by position
  • The + operator appends the items in the second list to the end of

the first list.

slide-15
SLIDE 15

Dictionary objects

slide-16
SLIDE 16

Dictionaries

  • Dictionaries are an unordered data structure.
  • They're defined using curly brackets: {}
  • Values are identified by keys. In this example, the keys

are identifiers for the values

  • We "look up" values in the dictionary using the keys.

'widget' 'flange' 'smoke shifter' 'poiuyt'

catalog = {'1008':'widget', '2149':'flange', '19x5':'smoke shifter', '992':'poiuyt'} catalog['19x5'] catalog['1008']

value '1008' '2149' '19x5' '992' key key

slide-17
SLIDE 17

Dictionaries

  • Keys can also represent characteristics of an object
  • Keys are always strings, values can be any object type
  • "dict" is Python slang for "dictionary"

Mickey Mouse' 'Disney' True 8

profile = {'name':'Mickey Mouse', 'company':'Disney', 'animated':True, 'fingers':8} profile['animated'] profile['name']

value 'name' 'company' 'animated' 'fingers' key key

slide-18
SLIDE 18

Commands for editing dictionaries

  • An empty dictionary can be created using

traits = {}

  • Both creating and changing a value in the dictionary are done by

assigning a value by designated key traits['height'] = 12

  • An item can be removed using the del command

del traits['eye color']