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.

Slides:



Advertisements
Similar presentations
ThinkPython Ch. 10 CS104 Students o CS104 n Prof. Norman.
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.
Guide to Programming with Python
Python Dictionary.
Dictionaries Last half of Chapter 5. Dictionary A sequence of key-value pairs – Key is usually a string or integer – Value can be any python object There.
Sequences The range function returns a sequence
Lists Introduction to Computing Science and Programming I.
1 Sequences A sequence is a list of elements Lists and tuples – Lists mutable – Tuples immutable Sequence elements can be indexed with subscripts – First.
JaySummet IPRE Python Review 2. 2 Outline Compound Data Types: Strings, Tuples, Lists & Dictionaries Immutable types: Strings Tuples Accessing.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
Guide to Programming with Python
Data Structures Akshay Singh.  Lists in python can contain any data type  Declaring a list:  a = [‘random’,’variable’, 1, 2]
COMPUTER SCIENCE FEBRUARY 2011 Lists in Python. Introduction to Lists Lists (aka arrays): an ordered set of elements  A compound data type, like strings.
Python Data Structures
Python Programming Chapter 10: Dictionaries Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
CS 177 Week 11 Recitation Slides 1 1 Dictionaries, Tuples.
Lists in Python.
October 17, 2005ICP: Chapter 5: Lists and Dictionaries 1 Introduction to Computer Programming Chapter 5: Lists and Dictionaries Michael Scherger Department.
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.
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.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 14 Tuples, Sets, and Dictionaries 1.
Python Lists and Such CS 4320, SPRING List Functions len(s) is the length of list s s + t is the concatenation of lists s and t s.append(x) adds.
Beyond Lists: Other Data Structures CS303E: Elements of Computers and Programming.
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 and Tuples Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Built-in Data Structures in Python An Introduction.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
Tuples and Dictionaries Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
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.
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.
7. Lists 1 Let’s Learn Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
Dictionaries Intro to Computer Science CS 1510 Dr. Sarah Diesburg.
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. The compound types you have learned about - - strings, lists, and tuples – use integers as indices. If you try to use any other type as.
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.
PYTHON PROGRAMMING. WHAT IS PYTHON?  Python is a high-level language.  Interpreted  Object oriented (use of classes and objects)  Standard library.
Today… Files from the Web! Dictionaries. Lists of lists. Winter 2016CISC101 - Prof. McLeod1.
Dictionaries Alexandra Stefan CSE1310 – University of Texas at Arlington.
Guide to Programming with Python Chapter Four Strings, and Tuples; for Loops: The Word Jumble Game.
Section06: Sequences Chapter 4 and 5. General Description "Normal" variables x = 19 – The name "x" is associated with a single value Sequence variables:
String and Lists Dr. José M. Reyes Álamo.
Intro to CS Nov 21, 2016.
CMSC201 Computer Science I for Majors Lecture 17 – Dictionaries
CMSC201 Computer Science I for Majors Lecture 21 – Dictionaries
When to use Tuples instead of Lists
Containers and Lists CIS 40 – Introduction to Programming in Python
COSC 1323 – Computer Science Concepts I
Bryan Burlingame 03 October 2018
Bryan Burlingame Halloween 2018
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
CEV208 Computer Programming
String and Lists Dr. José M. Reyes Álamo.
Reference semantics, variables and names
CHAPTER 4: Lists, Tuples and Dictionaries
Bryan Burlingame Halloween 2018
Python Review
Winter 2019 CISC101 5/26/2019 CISC101 Reminders
Lists [] First = [1, 2, 3, 4] Second = list[range(1,5)]
Dictionary.
Tuple.
Presentation transcript:

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 type, which use integers as indices to access the values they contain within them. because their items occur in order.

Compound data types Dictionaries are yet another kind of compound type. They are Python’s built-in mapping type. They maps key, which can by any immutable type, to values, which can be any type, just like the elements of a list or tuple. They are call associative arrays since they associate a key with a value.

DICTIONARIES As a example, we will create a dictionary to translate English words into Spanish. For this dictionary, the keys are strings. One way to create a dictionary is to start with the empty dictionary and add key : value pairs. The empty is denoted {}:

A dictionary to translate English word to spanish The first assignment create a dictionary name s. The other assignments add new key-value pairs to the dictionary.

A dictionary to translate English word to spanish The key-value pairs of the dictionary are seperated by commas. Each pair contains a key and a value separated by a colon

The Order of the Pairs The order of the pairs may not be what you expected. Python uses complex algorithms, designed for very fast access, to determine where the key-value pairs are stored in a dictionary. For our purposes we can think of this ordering as unpredicatable.

Another way to create a dictionary Another way to create a dictionary is to provide a list of key-value pairs using the same syntax as the previous output: It doesn’t matter what order we write the pairs. The values in a dictionary are accessed with keys, not with indices, so there is no need to care about ordering.

The Compound Non-Sequence Type The dictionary is the first compound type that we’ve seen that is not a sequence, so we can’t index or slice a dictionary.

Dictionary operations

del operation The del statement removes a key-value pair from a dictionary. For example, the dictionary contains the names of various fruits and the number of each fruit in stock: If someone buys all of the oranges, we can remove the entry from the dictionary:

Dictionary operations

Dictionaries operation A new shipment of bananas arriving could be handled like this:

Dictionary methods Dictionaries have a number of useful built-in methods. The keys method returns what Python 3 calls view of its underlying keys. A view object has some similarities to the range object.

Omit the keys method call in the for loop

The values method returns a view object which can be turned into a list

The items method returns a view, which promises a list of tuples - One tuple for each key: value pair:

Tuples are often useful for getting both the key and the value at the same time while we are looping

in and not in

Looking up a non-existent key

Alliasing and copying As in the case of lists, because dictionaries are mutable, you need to be aware of aliasing. Whenever two variables refer to the same object, changes to one affect the other.

If you want to modify a dictionary and keep a copy of the original, use the copy method. For example opposites is a dictionary that contains pairs of opposites: Alliasing and copying

alias and opposites refer to the same object; copy refers to a fresh copy of the same dictionary.

Linked List A linked list is either: 1. the empty list, represented by None, or 2. a node that contains a cargo object and a reference to a linked list.

The Node class