Dictionaries Dictionary: object that stores a collection of data

Slides:



Advertisements
Similar presentations
Dictionaries: Keeping track of pairs
Advertisements

Guide to Programming with Python
9 Dictionaries © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming 1.
DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.
Python Dictionary.
INLS 560 – D ICTIONARIES Instructor: Jason Carter.
Lecture 05 – Dictionaries.  Consider the following methods of sorting information in a sequence. What is the output produced by each program? 2COMPSCI.
Lecture 23 – Python dictionaries 1 COMPSCI 101 Principles of Programming.
Data Structures Akshay Singh.  Lists in python can contain any data type  Declaring a list:  a = [‘random’,’variable’, 1, 2]
Data Structures in Python By: Christopher Todd. Lists in Python A list is a group of comma-separated values between square brackets. A list is a group.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 14 Tuples, Sets, and Dictionaries 1.
Beyond Lists: Other Data Structures CS303E: Elements of Computers and Programming.
Built-in Data Structures in Python An Introduction.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
Daniel Jung. Types of Data Structures  Lists Stacks Queues  Tuples  Sets  Dictionaries.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 Dictionaries and Sets.
14. DICTIONARIES AND SETS Rocky K. C. Chang 17 November 2014 (Based on from Charles Dierbach, Introduction to Computer Science Using Python and Punch and.
Data Collections CS 127. Lists Lists are ordered sequences of items All programming languages provide a sequence structure similar to a Python list; in.
Guide to Programming with Python Chapter Five Lists and dictionaries (data structure); The Hangman Game.
CSC Introduction to Data Structures Devon M. Simmonds Computer Science Department University of North Carolina Wilmington Wilmington, NC 28403
LECTURE 3 Python Basics Part 2. FUNCTIONAL PROGRAMMING TOOLS Last time, we covered function concepts in depth. We also mentioned that Python allows for.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
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.
Dictionaries Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Python Data Structures By Greg Felber. Lists An ordered group of items Does not need to be the same type – Could put numbers, strings or donkeys in the.
Dictionaries Alexandra Stefan CSE1310 – University of Texas at Arlington.
Copyright © 2013 by John Wiley & Sons. All rights reserved. SETS AND DICTIONARIES CHAPTER Slides by James Tam Department of Computer Science, University.
More Python Data Structures  Classes ◦ Should have learned in Simpson’s OOP ◦ If not, read chapters in Downey’s Think Python: Think like a Computer Scientist.
CSc 110, Autumn 2016 Lecture 26: Sets and Dictionaries
COMPSCI 107 Computer Science Fundamentals
10 - Python Dictionary John R. Woodward.
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
CSc 110, Spring 2017 Lecture 29: Sets and Dictionaries
CMSC201 Computer Science I for Majors Lecture 21 – Dictionaries
When to use Tuples instead of Lists
CSc 110, Autumn 2016 Lecture 27: Sets and Dictionaries
Department of Computer Science,
Data types: Complex types (Dictionaries)
Dictionaries, File operations
CSc 110, Spring 2018 Lecture 32: Sets and Dictionaries
CSc 110, Autumn 2017 Lecture 31: Dictionaries
CSc 110, Autumn 2017 Lecture 30: Sets and Dictionaries
CISC101 Reminders Quiz 2 this week.
Topics Introduction to File Input and Output
CSc 110, Spring 2018 Lecture 33: Dictionaries
Bryan Burlingame Halloween 2018
CHAPTER THREE Sequences.
Guide to Programming with Python
CS190/295 Programming in Python for Life Sciences: Lecture 6
6. Lists Let's Learn Python and Pygame
4. sequence data type Rocky K. C. Chang 16 September 2018
Topics Sequences Introduction to Lists List Slicing
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
CS 1111 Introduction to Programming Fall 2018
6. Dictionaries and sets Rocky K. C. Chang 18 October 2018
Dictionaries: Keeping track of pairs
Topics Sequences Lists Copying Lists Processing Lists
CISC101 Reminders Assignment 2 due today.
CHAPTER 4: Lists, Tuples and Dictionaries
15-110: Principles of Computing
Bryan Burlingame Halloween 2018
Topics Sequences Introduction to Lists List Slicing
CSE 231 Lab 8.
Winter 2019 CISC101 5/26/2019 CISC101 Reminders
Topics Introduction to File Input and Output
Introduction to Dictionaries
Dictionary.
Tuple.
Introduction to Computer Science
Presentation transcript:

Dictionaries Dictionary: object that stores a collection of data Each element consists of a key and a value Often referred to as mapping of key to value Key must be an immutable object To retrieve a specific value, use the key associated with it Format for creating a dictionary dictionary = {key1:val1, key2:val2}

Retrieving a Value from a Dictionary Elements in dictionary are unsorted General format for retrieving value from dictionary: dictionary[key] If key in the dictionary, associated value is returned, otherwise, KeyError exception is raised Test whether a key is in a dictionary using the in and not in operators Helps prevent KeyError exceptions

Adding Elements to an Existing Dictionary Dictionaries are mutable objects To add a new key-value pair: dictionary[key] = value If key exists in the dictionary, the value associated with it will be changed

# The add function adds a new entry into the # birthdays dictionary. def add(birthdays): # Get a name and birthday. name = input('Enter a name: ') bday = input('Enter a birthday: ') # If the name does not exist, add it. if name not in birthdays: birthdays[name] = bday else: print('That entry already exists.')

Deleting Elements From an Existing Dictionary To delete a key-value pair: del dictionary[key] If key is not in the dictionary, KeyError exception is raised

# The delete function deletes an entry from the # birthdays dictionary. def delete(birthdays): # Get a name to look up. name = input('Enter a name: ') # If the name is found, delete the entry. if name in birthdays: del birthdays[name] else: print('That name is not found.')

Getting the Number of Elements and Mixing Data Types len function: used to obtain number of elements in a dictionary Keys must be immutable objects, but associated values can be any type of object One dictionary can include keys of several different immutable types Values stored in a single dictionary can be of different types

Creating an Empty Dictionary and Using for Loop to Iterate Over a Dictionary To create an empty dictionary: Use {} Use built-in function dict() Elements can be added to the dictionary as program executes Use a for loop to iterate over a dictionary General format: for key in dictionary:

Some Dictionary Methods clear method: deletes all the elements in a dictionary, leaving it empty Format: dictionary.clear() get method: gets a value associated with specified key from the dictionary Format: dictionary.get(key, default) default is returned if key is not found Alternative to [] operator Cannot raise KeyError exception

Some Dictionary Methods (cont’d.) items method: returns all the dictionaries keys and associated values Format: dictionary.items() Returned as a dictionary view Each element in dictionary view is a tuple which contains a key and its associated value Use a for loop to iterate over the tuples in the sequence Can use a variable which receives a tuple, or can use two variables which receive key and value

Some Dictionary Methods (cont’d.) keys method: returns all the dictionaries keys as a sequence Format: dictionary.keys() pop method: returns value associated with specified key and removes that key-value pair from the dictionary Format: dictionary.pop(key, default) default is returned if key is not found

Some Dictionary Methods (cont’d.) popitem method: returns a randomly selected key-value pair and removes that key-value pair from the dictionary Format: dictionary.popitem() Key-value pair returned as a tuple values method: returns all the dictionaries values as a sequence Format: dictionary.values() Use a for loop to iterate over the values

Some Dictionary Methods (cont’d.)