Dictionary in python programming language

Python dictionary

In this tutorial, we will discuss the dictionary in the python programming language.

Dictionary is a special type of variable in python. It is an unordered collection of items, it has a pair of items, key and value. When creating the dictionary, we can place items inside of the dictionary using  curly braces{} and separated by a comma.

Every item has a key and the corresponding value expressed as a pair key: value

Dictionary in python

How to create the dictionary

Dictionary can have items inside curly braces {} and every item is separated by the comma.
Dictionary can have a pair of items such as key and value.

ProgrammingLanguages={}
ProgrammingLanguages[“php”]=”Hyper text Preprocessor”
ProgrammingLanguages[“htm”]=”Hyper text markup language”
ProgrammingLanguages[“css”]=”cascading style sheet”
ProgrammingLanguages[“js”]=”Java script”

How to accessing Dictionary

How to print all of element of dictionary

print(ProgrammingLanguages) //print all element of dictionary

Output

{‘php’: ‘Hyper text Preprocessor’, ‘html’: ‘Hyper text markup language’, ‘css’: ‘cascading style sheet’, ‘js’: ‘Java script’}

Print  every element of using name instead index value or key value

Output                                   //print every element of dictionary using keys

Hypertext Preprocessor
Hypertext markup language
cascading style sheet
Java script

How to update an item of dictionary

If we want to update items to the dictionary, we can use an assignment operator. An example is given below


When the above code is executed, it produces the following results


{'age': 45, 'name': 'saman'} // before update
{'age': 34, 'name': 'saman'} //after update
How to add the item of dictionary


If we want to add items to the dictionary, we can use the assignment operator. Example is given below


When the above code is executed, it produced the following results


{'age': 45, 'name': 'saman'}  // before add
{'age': 45, 'name': 'saman', 'address': 'Jaffna'} //after add


How to remove the item of dictionary

If we want to remove items from the dictionary, we can use pop() operator. Example is given below

remove particular item using pop
When the above code is executed, it produced the following results
{'age': 45, 'name': 'saman', 'address': 'colombo'}

45

{'name': 'saman', 'address': 'colombo'}


remove any item using pop

 

 

When the above code is executed, it produced the following results
{'age': 45, 'name': 'saman', 'address': 'colombo'}

('age', 45)

{'name': 'saman', 'address': 'colombo'}


How to clear or delete dictionary items using del, clear()


When the above code is executed, it produced the following results

{1: 1, 2: 3, 3: 6, 4: 10, 5: 15}
{1: 1, 2: 3, 3: 6, 5: 15}

{}

 

Related post
Data type in Python                                 List in Python    
Tuple in Python                                        

 

 

Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

3 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

3 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

4 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

4 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

4 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

4 months ago

This website uses cookies.