Skip to main content

Python Dictionaries

Python Dictionaries
Here, we will learn about dictionaries, the operators and methods used on dictionaries. It is nearly inconceivable to work with python program or scripts, without lists and dictionaries. Python dictionaries can be changed easily at runtime.
Dictionary is like a list, but in a general form. It can be think like this, dictionary is a mapping between set of indexes or keys with set of values, where each key maps to a value. Combination of a key and its value is called a key value pair or item.
Suppose we take an example, and build a dictionary which maps words from English to French. So the keys and their values will be all strings.
In Python dictionary, each key is separated by a colon (:) from its values. All the items are separated by comas and the whole dictionary is enclosed within ‘{‘ and ‘}’. In the dictionary, all the keys should have to be unique with data type as strings, tuples or numbers and the values can be of any of these three type.

Define An Dictionary In Python

Example:
#!/usr/bin/python

dicto
= {'Bookname' : 'Python', 'Price' : 210}

Accessing Dictionary Values

Dictionaries values can be accessed by using the square braces i.e. [ ] along with the key name to obtain the value. This is multiple item declaration procedure used to declare keys along with their values in Python’s dictionary.
Example:
#!/usr/bin/python

dicto
= {'Bookname' : 'Python', 'Price' : 210}
print dicto['Bookname']
print dicto['Price']
When the above code gets executed the outcome will be:
Output:
Python
260

Creating A New Dictionary In Python

Example:
#!/usr/bin/python

new_dict
= dict()
print new_dict
or simply write
#!/usr/bin/python

new_dict
= {}
print new_dict
Output:
{}
The function ‘dict’ creates a new dictionary with no items within it. This is because ‘dict’ is the name of a built-in function and programmers should avoid in using it as variable name. Furthermore the curly braces { & }  together represents an empty dictionary. So to use the dictionary to add items Python programmers use square braces such as:
Example:
#!/usr/bin/python
new_dict
= dict()

new_dict
['First'] = 10;
new_dict
['Second'] = 20;
For mapping a single item of a dictionary, programmers can use this procedure also. This is one-by-one procedure of declaring items.  The line creates an item that maps from key ‘first’ to the value 10. Similarly in second case also.

Dictionary As Set Of Counter

Let us consider a situation where programmers are given and they have to count how many times each letter appears.
To do this the techniques are:
  • Create twenty six variables, one for each letter of the alphabet & make the string traverse for each character, incrementing the corresponding counter variable.
  • Using a list, containing twenty six elements & converting each letter to a specific number & use the number as an index to the list.
  • Programmers can also use dictionaries with letters as keys & counter as their corresponding values & adding those items in the dictionary. Then, the programmer can increment the value of an existing item based on the repetition of every single letter.
It is based on the programmer what & how he/she wants to implement things into programming. An implementation is the method of doing or performing a computation based on your conception. In the above scenario, there is an advantage while using dictionaries – that we do not have to think or know ahead of which letter appears in the string & just have to allot space & room for those letters.

Updating Dictionary In Python

Programmers can update or modify the existing dictionary by simply adding new entry or a key-value pair or by deleting an item or entry.
The code describes it below:
Example:
#!/usr/bin/python

dicto
= {'Bookname' : 'Python', 'Price' : 210}

#Adding new entries
dicto
['Author'] = 'TutorialsCloud' ;
dicto
['Discount']= '10 Percent';

#Updating an Entry
dicto
['Price']  = 200;

Deleting Elements From Dictionary

Deleting elements from a dictionary can be done either removing individual elements or use clear() to clear the entire dictionary’s content.
Example is show below:
Example:
#!/usr/bin/python
dicto
= {'Bookname' : 'Python', 'Price' : 210, 'Author': 'TutorialsCloud'}

del dicto['Price'];
# It deletes only the key with the name 'Price'

dicto
.clear();
# The above code removes all entries in dictionary & makes the dictionary empty

del dicto
# The above code Deletes the entire dictionary

Comments

For Programs Click Here

Popular posts from this blog

Syllabus

Syllabus  C Programming Tutorials C Tutorial C Introduction History of C Programming Language C Installation C Program Structure C Input and Output (I/O) C Format Specifiers Declaration & Assignments C Tokens C Identifiers C Keywords C Constants C Operators C Data Types C Variables C Preprocessors C Type Casting C Custom Header File Flow Control C Decision Making C if Statements C if-else Statements C Nested if-else Statements C else-if Statements C goto Statement C switch Statements C Loops C while loops C do while loops C for loops Functions C Functions C Function Arguments C Library Functions C Variable Scope Arrays & Strings C Arrays C Strings Pointers C Pointers C Dynamic Memory Allocation Structure & Union C Structures C Unions File I/O C File Handling C fopen C fclose C getc C putc C getw C putw C fprintf C fscanf C fgets C fputs C feof                                     ...

Syllabus

Python Tutorials Python Tutorial Python Overview Python Installation Basics of Python Programming Python Operators Python Keywords Python Numbers Python Strings Python Data Types Python Variables Python Lists Python Tuples Python Date and Time Python Decision Making Python Loops Python File Handling Python Dictionaries Python Functions Python Modules Python Exceptions Handling Python Object Oriented Inheritance in Python Python Regular Expressions Python Networking Programming Python Multithreaded Programming Python CGI Programming Python Database Connection Python Metaprogramming Python Data Processing And Encoding Python GUI Programming

C++ Data Abstraction

C++ Data Abstraction Object oriented programming offers various features to write programs with various concepts that help to minimize problems and increase flexibility in program. One of the features of object oriented programming is Data abstraction. In this chapter you will learn about how the concept data abstraction is carried out within the C++ program. What is Data abstraction? Data abstraction allows a program to ignore the details of how a data type is represented. Abstraction (derived from a Latin word  abs , meaning away from and  trahere , meaning to draw) refers to the act of representing essential features without including the background details or explanations. C++ classes use the technique of abstraction and are defined as a list of abstract attributes such as width, cost, size etc and functions to operate on these attributes. They put in a nutshell all the essential properties of an object that are required to be created. The attributes are therefore called...