SlideShare a Scribd company logo
 
 
 
Python 3.x - Dictionaries and Sets 
Cheatsheet 
 
 
   
1
Commonly used Dictionaries Manipulation Commands
● Declare an empty dictionary:​ ​d = ​dict​()​ or​ ​d = ​{}
● Create a dictionary with pre-existing key-value pairs: ​d = ​{​<key1>​: ​<value1>​, ​<key2>​:
<value2>​, ​<key3>​: ​<value3>​, ….​}
● Computing number of entries in dictionary: ​len​(​d​)
● Add new key-value pair to dictionary: ​d​[​<key>​]​ = ​value
● Iterate over key-value pairs:
for​ ​<key>​, ​<value>​ ​in​ d.items​()​:
​print​(​"Key:"​, ​<key>​, ​" Value:"​, ​<value>​)
● Iterate over keys:
for​ <key>​ ​in​ d.keys​()​:
​print​(​"Key:"​,​ <key>​)
● Iterate over values:
for​ ​<value>​ ​in​ d.values​()​:
​print​(​"Value:"​, ​<value>​)
● Remove an item from dictionary: ​del​ d​[​<key>​]
● Remove an item from dictionary and return that to a variable using its key: ​var = d.pop​(​<key>​)
● Remove an item from dictionary and return it as a tuple using key-value pair of an entry in it: ​var =
d.popitem​(​<key>​, ​<value>​)
● Alternative access to a key-value pair. This method will also not throw ​KeyError exception instead a
default which can be set(second parameter of the method) by the user or throws None:
d.get​(​<key>​,​ ​"No match found!"​)
● Create dictionary from sequence of keys: ​d.fromkeys​(​<keys(can be in the form of list,
tuple or set)>​, ​<value>​)
● Update dictionary - either you can change existing entry in the dictionary or add a new entry to the
dictionary: ​d.update​(​{​'existing key'​: ​<new_value>​, ​'existing key2'​:
<new_value2>​, ​'new key'​: ​<new_value>​....​}​)
● Create dictionary comprehension from arbitrary key and value expressions: ​{​x: x**2 ​for x ​in
(​2, 4, 6, <any other numbers>....​)​}
● Return a shallow copy of dictionary to a variable:​ ​var = d.copy​()
● Clear all entries from the dictionary:​ ​d.clear​()
2
Commonly used Sets Manipulation Commands 
Note: Sets are used to remove duplicate items. It doesn't care about the order of the elements inside it as it
always displays its items randomly​.
● Declaration of a new set:​ ​s = ​set​()
● Adding a new element to the set. There is no effect the element is already present in the set:
s.add​(​<Element to be added>​)
● Finding elements which are common between two or more sets: ​s.intersection​(​<set1 to be
compared​, ​<set2 to be compared>​....​)
● Finding elements which are not common between two or more sets:​ ​s.difference​(​<set1 to be
compared​, ​<set2 to be compared>​....​)
● Combining two or more set:​ ​s.union​(​<set1 to be combined>​, ​<set2 to be
combined>​....​)
● Remove all elements of another set(s) from a set:​ ​s.difference_update​(​<another set1>​,
<another set2>​....​)
● Update set with the intersection itself and another set(s):​ ​s.intersection_update​(​<another
set1>​,​ <another set2>​....​)
● Update a set with the union of itself and another set(s):​ ​s.update​(​<another set1>​, ​<another
set2>​....​)
● Clear all the elements from a set:​ ​s.clear​()
● Return a shallow copy of a set:​ ​s.copy​()
● Return elements between two sets(not more than that) that are not common among each other. This is
known as symmetric difference between two sets:​ ​s.symmetric_difference​(​<another set>​)
● Returns ​True​ if two sets are having intersection ​None​: ​s.isdisjoint​(​<another set>​)
● Returns ​True​ if another set contains this set: ​s.issubset​(​<another set>​)
● Returns ​True​ if a set contains another set: ​s.issuperset​(​<another set>​)
● Update a set with its symmetric difference of another set:
s.symmetric_difference_update​(​<another set>​)
3

More Related Content

PDF
[1062BPY12001] Data analysis with R / April 26
PDF
JavaScript Fundamentals with Angular and Lodash
PDF
Lodash js
PPTX
Python Spell Checker
PDF
Chaining and function composition with lodash / underscore
PDF
Property-based testing
PDF
Clojure functions midje
KEY
Into Clojure
[1062BPY12001] Data analysis with R / April 26
JavaScript Fundamentals with Angular and Lodash
Lodash js
Python Spell Checker
Chaining and function composition with lodash / underscore
Property-based testing
Clojure functions midje
Into Clojure

What's hot (20)

ODP
Scala introduction
PDF
PDF
PDF
React Back to the Future
PDF
Python For Data Science Cheat Sheet
TXT
Array operators
PDF
PDF
What Have The Properties Ever Done For Us
PDF
Pymongo for the Clueless
PDF
ESNext for humans - LvivJS 16 August 2014
PDF
The Ring programming language version 1.6 book - Part 35 of 189
PDF
Programmation fonctionnelle en JavaScript
ODP
Python Day1
PDF
Introduction to Python
PPT
Synapse india complain sharing info about php chaptr 26
PDF
Migrating to dependency injection
PDF
Understanding Lemon Generated Parser Final
PDF
Understanding Lemon Generated Parser
Scala introduction
React Back to the Future
Python For Data Science Cheat Sheet
Array operators
What Have The Properties Ever Done For Us
Pymongo for the Clueless
ESNext for humans - LvivJS 16 August 2014
The Ring programming language version 1.6 book - Part 35 of 189
Programmation fonctionnelle en JavaScript
Python Day1
Introduction to Python
Synapse india complain sharing info about php chaptr 26
Migrating to dependency injection
Understanding Lemon Generated Parser Final
Understanding Lemon Generated Parser
Ad

Similar to Python 3.x Dictionaries and Sets Cheatsheet (20)

PDF
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
PPTX
Python_Sets(1).pptx
PDF
Set methods in python
PPTX
Seventh session
PPTX
SETS IN PYTHON-157755566677778⁵567886676.pptx
PPTX
Python introduction data structures lists etc
PPTX
237R1A6712.Python.pptxhhhhhhhhhhhhhhhhhh
PPTX
Python Sets_Dictionary.pptx
PPTX
6Dictionaries and sets in pythonsss.pptx
PPTX
Dictionaries and Sets
PPTX
ch 13 Dictionaries2.pptx
PDF
Python Dictionary
PPT
Tuples, sets angsdfgsfdgdfgfdgd dictionaries 2.ppt
PPTX
Python programming -Tuple and Set Data type
PPTX
dictionary14 ppt FINAL.pptx
PPT
Tuples, sets and dictionaries-Programming in Python
PPTX
Chapter 3-Data structure in python programming.pptx
PPTX
Python dictionary
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
Python_Sets(1).pptx
Set methods in python
Seventh session
SETS IN PYTHON-157755566677778⁵567886676.pptx
Python introduction data structures lists etc
237R1A6712.Python.pptxhhhhhhhhhhhhhhhhhh
Python Sets_Dictionary.pptx
6Dictionaries and sets in pythonsss.pptx
Dictionaries and Sets
ch 13 Dictionaries2.pptx
Python Dictionary
Tuples, sets angsdfgsfdgdfgfdgd dictionaries 2.ppt
Python programming -Tuple and Set Data type
dictionary14 ppt FINAL.pptx
Tuples, sets and dictionaries-Programming in Python
Chapter 3-Data structure in python programming.pptx
Python dictionary
Ad

More from Isham Rashik (20)

PDF
Text Preprocessing - 1
PDF
9608 Computer Science Cambridge International AS level Pre-Release May June p...
PDF
Fundamentals of Cryptography - Caesar Cipher - Python
PDF
HackerRank Repeated String Problem
PDF
Operations Management - BSB INC - Case Study
PDF
Corporate Finance - Disney Sea Park Project
PDF
Questionnaire - Why women entrepreneurs are happier than men?
PDF
Human Resource Management - Different Interview Techniques
PDF
Python 3.x File Object Manipulation Cheatsheet
PDF
Android Application Development - Level 3
PDF
Android Application Development - Level 2
PDF
Android Application Development - Level 1
PDF
Managerial Skills Presentation - Elon Musk
PDF
Operations Management - Business Process Reengineering - Example
PPTX
Lighting Design - Theory and Calculations
PDF
Linear Control Hard-Disk Read/Write Controller Assignment
PDF
Transformers and Induction Motors
PDF
Three phase balanced load circuits and synchronous generators
PPTX
Circuit Breakers for Low Voltage Applications
PDF
Linux Commands - Cheat Sheet
Text Preprocessing - 1
9608 Computer Science Cambridge International AS level Pre-Release May June p...
Fundamentals of Cryptography - Caesar Cipher - Python
HackerRank Repeated String Problem
Operations Management - BSB INC - Case Study
Corporate Finance - Disney Sea Park Project
Questionnaire - Why women entrepreneurs are happier than men?
Human Resource Management - Different Interview Techniques
Python 3.x File Object Manipulation Cheatsheet
Android Application Development - Level 3
Android Application Development - Level 2
Android Application Development - Level 1
Managerial Skills Presentation - Elon Musk
Operations Management - Business Process Reengineering - Example
Lighting Design - Theory and Calculations
Linear Control Hard-Disk Read/Write Controller Assignment
Transformers and Induction Motors
Three phase balanced load circuits and synchronous generators
Circuit Breakers for Low Voltage Applications
Linux Commands - Cheat Sheet

Recently uploaded (20)

PDF
Computing-Curriculum for Schools in Ghana
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Insiders guide to clinical Medicine.pdf
PDF
Basic Mud Logging Guide for educational purpose
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Pre independence Education in Inndia.pdf
PPTX
master seminar digital applications in india
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Cell Structure & Organelles in detailed.
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
01-Introduction-to-Information-Management.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Complications of Minimal Access Surgery at WLH
Computing-Curriculum for Schools in Ghana
Pharmacology of Heart Failure /Pharmacotherapy of CHF
VCE English Exam - Section C Student Revision Booklet
FourierSeries-QuestionsWithAnswers(Part-A).pdf
O7-L3 Supply Chain Operations - ICLT Program
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Insiders guide to clinical Medicine.pdf
Basic Mud Logging Guide for educational purpose
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Pre independence Education in Inndia.pdf
master seminar digital applications in india
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
human mycosis Human fungal infections are called human mycosis..pptx
Anesthesia in Laparoscopic Surgery in India
Cell Structure & Organelles in detailed.
2.FourierTransform-ShortQuestionswithAnswers.pdf
01-Introduction-to-Information-Management.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPH.pptx obstetrics and gynecology in nursing
Complications of Minimal Access Surgery at WLH

Python 3.x Dictionaries and Sets Cheatsheet

  • 1.       Python 3.x - Dictionaries and Sets  Cheatsheet          1
  • 2. Commonly used Dictionaries Manipulation Commands ● Declare an empty dictionary:​ ​d = ​dict​()​ or​ ​d = ​{} ● Create a dictionary with pre-existing key-value pairs: ​d = ​{​<key1>​: ​<value1>​, ​<key2>​: <value2>​, ​<key3>​: ​<value3>​, ….​} ● Computing number of entries in dictionary: ​len​(​d​) ● Add new key-value pair to dictionary: ​d​[​<key>​]​ = ​value ● Iterate over key-value pairs: for​ ​<key>​, ​<value>​ ​in​ d.items​()​: ​print​(​"Key:"​, ​<key>​, ​" Value:"​, ​<value>​) ● Iterate over keys: for​ <key>​ ​in​ d.keys​()​: ​print​(​"Key:"​,​ <key>​) ● Iterate over values: for​ ​<value>​ ​in​ d.values​()​: ​print​(​"Value:"​, ​<value>​) ● Remove an item from dictionary: ​del​ d​[​<key>​] ● Remove an item from dictionary and return that to a variable using its key: ​var = d.pop​(​<key>​) ● Remove an item from dictionary and return it as a tuple using key-value pair of an entry in it: ​var = d.popitem​(​<key>​, ​<value>​) ● Alternative access to a key-value pair. This method will also not throw ​KeyError exception instead a default which can be set(second parameter of the method) by the user or throws None: d.get​(​<key>​,​ ​"No match found!"​) ● Create dictionary from sequence of keys: ​d.fromkeys​(​<keys(can be in the form of list, tuple or set)>​, ​<value>​) ● Update dictionary - either you can change existing entry in the dictionary or add a new entry to the dictionary: ​d.update​(​{​'existing key'​: ​<new_value>​, ​'existing key2'​: <new_value2>​, ​'new key'​: ​<new_value>​....​}​) ● Create dictionary comprehension from arbitrary key and value expressions: ​{​x: x**2 ​for x ​in (​2, 4, 6, <any other numbers>....​)​} ● Return a shallow copy of dictionary to a variable:​ ​var = d.copy​() ● Clear all entries from the dictionary:​ ​d.clear​() 2
  • 3. Commonly used Sets Manipulation Commands  Note: Sets are used to remove duplicate items. It doesn't care about the order of the elements inside it as it always displays its items randomly​. ● Declaration of a new set:​ ​s = ​set​() ● Adding a new element to the set. There is no effect the element is already present in the set: s.add​(​<Element to be added>​) ● Finding elements which are common between two or more sets: ​s.intersection​(​<set1 to be compared​, ​<set2 to be compared>​....​) ● Finding elements which are not common between two or more sets:​ ​s.difference​(​<set1 to be compared​, ​<set2 to be compared>​....​) ● Combining two or more set:​ ​s.union​(​<set1 to be combined>​, ​<set2 to be combined>​....​) ● Remove all elements of another set(s) from a set:​ ​s.difference_update​(​<another set1>​, <another set2>​....​) ● Update set with the intersection itself and another set(s):​ ​s.intersection_update​(​<another set1>​,​ <another set2>​....​) ● Update a set with the union of itself and another set(s):​ ​s.update​(​<another set1>​, ​<another set2>​....​) ● Clear all the elements from a set:​ ​s.clear​() ● Return a shallow copy of a set:​ ​s.copy​() ● Return elements between two sets(not more than that) that are not common among each other. This is known as symmetric difference between two sets:​ ​s.symmetric_difference​(​<another set>​) ● Returns ​True​ if two sets are having intersection ​None​: ​s.isdisjoint​(​<another set>​) ● Returns ​True​ if another set contains this set: ​s.issubset​(​<another set>​) ● Returns ​True​ if a set contains another set: ​s.issuperset​(​<another set>​) ● Update a set with its symmetric difference of another set: s.symmetric_difference_update​(​<another set>​) 3