Social Icons

Wednesday, August 14, 2019

Python dictionary


1 - Define a dictionary in Python 

A dictionary is a Python implementation of a data structure similar to an associative array. A dictionary consists of a collection of key-value pairs. Each key-value pair has the key attached to its associated value. A dictionary can be defined by enclosing braces {} with a list of key-value pairs separated by commas.

Syntax:

dic = {key1: value1, key2: value2, key3: value3, ...}
To access a value from the dictionary, use the name of the dictionary followed by the corresponding key in square brackets:

Example

dic = {key1: value1, key2: value2, key3: value3, ...}
print (dic [key1]) # this will display : value1

Example: Telephone Directory

phoneBook = {"Majid": "0556683531", "Tomas": "0537773332", "Bernard": "0668793338", "Hafid": "066445566"}
print(phoneBook ["Majid"]) # displays 0556683531

2 - Browse the values ​​and keys of a Python dictionary 

A Python dictionary has a method called values() that allows you to browse its values, and another named keys() to browse its keys.

Example: scans values ​​from a dictionary


phoneBook = { "Majid": "0556633558", "Tomas": "0587958414", "Bernard": "0669584758"}
for value in phoneBook.values():
    print(value)

Example: browse dictionary keys

phoneBook = { "Majid": "0556633558", "Tomas": "0587958414", "Bernard": "0669584758"}
for key in phoneBook.keys ():
    print(key)

Note : 

You can also browse keys and values ​​at the same time by going to the items() method

Example: walk through keys and values

phoneBook = { "Majid": "0556633558", "Tomas": "0587958414", "Bernard": "0669584758"}
for key, value in phoneBook.items ():
    print (key, value)

3 - Update, add or delete elements of a dictionary 

3.1 Update a dictionary element 

You can update a dictionary item directly by assigning a value to a key:

Example: Stock Manager

stock = {"Laptop": 15, "Printer": 35, "Tablet": 27}
#modification of the value associated with the key "Printer"
stock [ "Printer"] = 42
print (stock)
# displays: {'Laptop': 15, 'Printer': 42, 'Tablet': 27}

3.2 - Add an element to the dictionary 

In the case of a non-existent key, the same method mentioned above, allows to add elements to the dictionary:

Example: Add an item to the stock

stock = {"Laptop": 15, "Printer": 35, "Tablet": 27}
# Added the "Ipad" element: 18
stock [ "Ipad"] = 18
print (stock)
# displays: {'Laptop': 15, 'Printer': 35, 'Tablet': 27, 'Ipad': 18}

3.3 - Delete an element from the dictionary 

You can remove an element from the dictionary by specifying its key in the pop() method

Example: deleting a dictionary element

stock = {'Laptop': 15, 'Printer': 35, 'Tablet': 27, 'Ipad': 22}
# Delete the "Printer" element: 35
stock.pop ( "Printer")
print (stock)
# displays: {'Laptop': 15, 'Tablet': 27, 'Ipad': 22}

Note

A dictionary has another method: popitem() that removes the last element

Example: Deleting the last item

stock = {'Laptop': 15, 'Printer': 35, 'Tablet': 27, 'Ipad': 22}
# Deleting the last element
stock.popitem ()
print (stock)
# displays: {'Laptop': 15, 'Printer': 35, 'Tablet': 27}

3.4 - Emptying a dictionary 

A Python dictionary can be dumped using the clear() method

Example: empty a dictionary

stock = {'Laptop': 15, 'Printer': 35, 'Tablet': 27, 'Ipad': 22}
# empty the dictionary
stock.clear ()
print (stock)
# displays an empty dictionary: {}

4 - Summary of dictionary associated methods 

Here is a summary of the main methods associated with a dictionary object:
  1. clear(): removes all items from the dictionary. 
  2. copy(): returns a shallow copy of the dictionary. 
  3. fromkeys (seq [, v]): Returns a new dictionary with the keys of seq and a value equal to v (the default is None). 
  4. get(key [, d]): returns the value of key. If the key does not exit, returns d (the default is None). 
  5. items(): returns a new view of dictionary items (key, value). 
  6. keys(): Returns a new view of the dictionary keys. 
  7. pop(key [, d]): removes the element with key and returns its value or d if key is not found. If d is not provided and the key can not be found, raises KeyError. 
  8. popitem(): delete and return an arbitrary element (key, value). Lift KeyError if the dictionary is empty. 
  9. setdefault(key [, d]): if key is in the dictionary, return its value. Otherwise, insert the key with the value d and return d (the default is None). 
  10. update([other]) - Update the dictionary with the key / value pairs of other existing keys. 
  11. values(): returns a new view of dictionary values


Younes Derfoufi

No comments:

Post a Comment

Category Of Mobile Courses

Actualités (644) Adsense (1) Affiliation (1) Algebraic Topology (1) Algorithmic (1) all-news (30) Android (5) Android App (8) Android app without code (4) Android Apps (169) Android Development (4) Android download (2) Android OS (3) AngularJS (1) Automata theory and formal language (5) C programming (5) Category and Functor (8) CMS (3) Computer Glossary (18) Create Mobile App With Ionic Framework (2) CSS (2) CSS-Cascading-Style-Sheets (4) Developpement Java (13) Differential Geometry (1) Django-Python-Framework (8) dropshiping (26) Earn Money by Internet (4) Emplois (23) Framework php (2) Fraud (2) HTML (7) Java For Beginners (10) Javascript (12) Kotlin Programming Language (8) Kotlin For Mobile Android (1) Linux Download (2) Marketing (5) Mobile (3) Mobile Courses (4) Mobile Marketing (4) MoneyGram (1) News (721) Node.js (5) Open Source (1) Photoshop (1) Protect Computer (1) Python (33) Python BeautifulSoup (1) Python For Data Science (2) Python-Books (6) Python-DVD-Training (1) Python-Exercises (138) Python-Framework (1) Python-IDE (1) Python-Modules (1) Python-pdf (2) Python-pyQt (1) Référencement (2) Script PHP (2) Security (6) SEO (1) Snipping Tool: Faq (1) Social Networks (1) Source Code (1) Statistics With SPSS (2) Surveillance Software (1) Travail à domicile (6) Tutoriels php en vidéos (2) Tutoriels-MySql (6) tutoriels-php (19) Utilitaires (1) VPS (1) Web Hosting (1) Webcam (1) Webmarketing (11) Western Union (1) Windows 10 (1) Windows 7 (4) Windows 7 Faq (2) Windows 8 (1) Windows Accessories (1) Windows Download (8) Windows Drivers (1) Windows Fonts (1) Windows Power Shell (2) Windows Registry (2) Windows Security (18) Windows Software (2) Windows Spyware (2) Windows utilities (3) Windows Virus (2) Windows Vista (3) Windows Wireless (1) Windows xp (1) Wordpress (1)
 

Sample text

Sample Text

 
Blogger Templates