0% found this document useful (0 votes)
24 views9 pages

9-Dictionaries and Sets

This document provides information on dictionaries and sets in Python. It defines dictionaries as unordered collections of key-value pairs that can be accessed by key. Sets are defined as unordered collections of unique and immutable objects. The document lists common dictionary and set functions and operations, such as retrieving values from dictionaries by key, performing set intersections and unions, adding/removing items from sets, and more. It provides examples of creating and manipulating dictionaries and sets in Python code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views9 pages

9-Dictionaries and Sets

This document provides information on dictionaries and sets in Python. It defines dictionaries as unordered collections of key-value pairs that can be accessed by key. Sets are defined as unordered collections of unique and immutable objects. The document lists common dictionary and set functions and operations, such as retrieving values from dictionaries by key, performing set intersections and unions, adding/removing items from sets, and more. It provides examples of creating and manipulating dictionaries and sets in Python code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

III B.Tech.

Computer Science & Engineering


CSE304: PYTHON PROGRAMMING WITH WEB
FRAMEWORKS
UNIT – II: Dictionaries and Sets

By
Mrs. S. KAMAKSHI, AP-III / CSE
School of Computing

26/08/20 PYTHON PROGRAMMING 1


Dictionary
• Unordered collections of arbitrary objects as
key-value pairs
• Accessed by key, not offset position
– Also called as associative arrays or hashes
– Associates a set of values with keys, so an item
can be fetched from a dictionary using the key
• Variable-length, heterogeneous, and
arbitrarily nestable
• Immutable key but mutable mapping
26/08/20 PYTHON PROGRAMMING 2
Dictionary
• Embedded within {} as key:value pairs delimited by
comma
– dict_name = {key1:value1, key2:value2, …}
• Eg.
countries = { “TN”:”Tamil Nadu”,
“TE”:”Telungana”,
“AP”: “Andra Pradesh”,
“KE”: “Kerala”,
“KA”: “Karnataka”}

movie = {1960:”Bala Nagamma”, 1961:”Jagathala Pradhapan”,


1963:”Lava Kusa”, 1964:”Karnan”, 1967: “Sri Krishnavatharam”}
26/08/20 PYTHON PROGRAMMING 3
Dictionary Functions
Method Description
dict_name[key] Returns the value of the specified key. If key does not exist throws
KeyError exception
get(key, default_value) Returns the value of the specified key
pop(key, default_value) Removes the element with the specified key
popitem() Removes the last inserted key-value pair
keys() Returns a view object that contains the dictionary's keys, reflects
changes done in dictionary
values() Returns a view object that contains all the values in the dictionary
items() Returns a view object that contains a tuple for each key value pair
clear() Removes all the elements from the dictionary
copy() Returns a copy of the dictionary
fromkeys(iterable, [value]) Returns a dictionary with the specified keys and value
setdefault(key, default_value) Returns the value of the specified key. If the key does not exist: insert
the key, with the specified value
update(iterable) Updates the dictionary with the specified key-value pairs
del dict_name[key] Removes the element with the specified key. If key does not exist
throws KeyError exception
26/08/20 PYTHON PROGRAMMING 4
Dictionary
• Retrieving value from dictionary
– dict_name[key]
– dict_name.get(key[, value])
– dict_name.set_default(key[, value])
• Checking for key in dictionary
– key in dict_name
• Converting between list and dictionary
– list(view_object)
– dict(2D-list or 2D-tuple)

26/08/20 PYTHON PROGRAMMING 5


Set
• Unordered collection of unique and immutable objects that supports
operations corresponding to mathematical set theory
• An item appears exactly once no matter how many times it is added to the
set
• sets are
– Iterable
– Can grow and shrink on demand
– May contain a heterogeneous types of objects

• To make a set object, pass in a sequence or other iterable object to the


built-in set function:
• Eg.
– x = set('abcde')
– y = set('bdxyz')
– x
{'a', 'c', 'b', 'e', 'd‘}
– X=set([1, 2, 3, 4]) # using constructor
– X
26/08/20
{1, 2, 3, 4} PYTHON PROGRAMMING 6
Set Operations
• x−y # Difference
• x|y # Union
• x&y # Intersection
• x^y # Symmetric difference (XOR)
• x > y, x < y # Superset, subset
• 'e' in x # Membership (sets)
• x.intersection(y) # Same as x & y
• x.union(y)

26/08/20 PYTHON PROGRAMMING 7


Set Operations
• z.add(item) # Insert one item
• z.update(iterable) # Merge: in-place
• z.remove('b') # Delete one item
• z.discard(‘b’) # Delete one item
• for item in s1:
print(item)
• len(s1)
• s1.issubset(s2)
• s1.issuperset(s2)
• s1.symmetric_difference(s2)
• s1.pop() # removes last item, since unordered
this is not recommended
26/08/20 PYTHON PROGRAMMING 8
Set Operations
• s1.clear() # removes all elements of set
• del s1 #deletes the set itself
• s1.copy() # returns a copy of the set

26/08/20 PYTHON PROGRAMMING 9

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy