Python dictionary tutorial

Python dictionaries are unordered collection of items used to store data values. It is changeable and indexed and is written in curly brackets and has keys and values. Keys are the single values. Values can be list or list within the list. The dictionaries are the data types which are similar to the array but it will work with the values and keys instead of indices. Each value stored in dictionary and can be accessed using the keys which act as object. The key value is provided in dictionary to make optimized and each key value and separated by colon’:’ there each key is separated by comma and it is enclosed in curly brace.
An empty dictionary is written with two curly braces like {}.
The dictionaries are unique and immutable data types as strings, integer and tuples. The values of dictionary may be of any type such as string, numbers. They provide another composite data type called as dictionary. The dictionaries are another example of data structures and used to map or associate things you want to store keys you need.

Dictionaries and list have characteristics:-

Both list and dictionaries are mutual.
Both list and dictionaries are dynamic and can grow and shrink as needed.
Both are nested and can contain another list and then vice versa.
Properties of dictionary keys are:-
There are some points that important while using the dictionary keys:-

  1. No duplicate key is allowed.
  2. The values in dictionary key can be of any type and immutable like tuples, numbers and strings.
  3. If the same key name is given with the different case they are treated as different keys in python dictionary.

Create dictionary:-

Creating the dictionary as simple placing item inside curly braces {} and are separated by the comma.
An item has key and corresponding value is expressed as pair, value.
The following steps to create the dictionaries as follows:-

  1. Firstly we have to create the new dictionary.# start with an empty one.
  2. Add value to dictionary.
  3. Then we have to remove the key and values.
  4. We have to check the length
  5. The next step to  test the dictionary
  6. Then the value of specific keys are gained
  7. Print the keys with a for loop.
  8. Lastly print ball the key and values.

Ways for creating dictionaries are as follows:-

  1. Empty dictionary:-

Creation of empty list is done by two methods as:-
#creation of the empty dictionaries by the empty braces.
e.g:-
my_dict={}
#creation of the empty dictionaries by using dict().
my_dict=dict()
O/p:-
{}
b)dictionary with literals:-
Creation of the dictionary by the set of keys of literals.
e.g:-
my_dict={1:’ball’,3:’apple’}
O/p:-
{1:’ball’, 3,’apple’}
c) Creation of dictionary by two lists:
suppose we have two sets:-
e.g:-
#list of strings
listofStrings = [“hi”, “hello”, “there”, “at”, “when”]
#list of integers
listofInts = [10, 45, 23, 77,67]
Now we will use elements in list of strings and item in list of int as value.
Zip function is used here to, merge the two sets.

O/p:-
{‘hi’:10,’hello’:45,’when’:67,’at’:77,’there’:23}

d) Creating dictionary by passing parameter in constructor:-
Now we create the dictionary by passing the values in dictionary constructors:-
e.g:-
my_dict=dict(ball=1,
this=22,
is=33,
are=27,
there=28)
O/p:-
{‘there’:28,’ball’:1,’this’:22,’is’:33,’are’:27}

E) Creating dictionary by list of initializing of keys by same values:-
Suppose we have list of strings as,
e.g:-
[“hi”,”hello”,”when”,”at”,”there”]
Now we create a dictionary where all elements of list will be key and default value will be 1.
O/p:-                   

{'hi': 1, 'hello': 1, 'when': 1, 'at': 1, 'there': 1}

Accessing elements from a dictionary:-

 

The accessing of the elements in dictionaries is as follows:-
While indexing with container type to access values dictionary uses keys.
Keys are used inside square bracket or get() method it will return none if  key not found.
e.g:-
my_dict = {'addr':'katraj', 'city': ‘pune’}
# Output:
katraj
print(my_dict['addr'])
# Output:
city
print(my_dict.get('pune'))
After running the code the
O/p:-
Katraj
City
Methods of dictionaries are given as follows:-

  1. Update values in dictionaries :-

e.g:-
      thisdict = {
      "brand": "honda",
      "model": "shine",
      "year": 2019
                 }
thisdict.update({"model": "Honda1"})

O/p:-
{‘brand’:’honda’,’model’:’shine’,’year’:2019,’color’:’blue’}

 

  1. To delete /pop item the pop()keyword is used remove last item:-

   thisdict = {
  "brand": "honda",
  "model": "shine",
  "year": 2019
                    }

thisdict.popitem()
print(thisdict)

 O/p:-

{‘brand’:’honda’,’model’:’shine’}

  1. To remove an arbitrary  item del()  keyword is used:-

 

e.g:-
thisdict = {
"brand": "honda",
 "model": "shine",
 "year": 2019
                 }

del thisdict["model"]
print(thisdict)

 O/p:

{‘brand’:’honda’,’year’:2019}

  1. To remove all item clear() keyword is used:-

 e.g:-
thisdict = {
 "brand": "honda",
 "model": "shine",
 "year": 2019
}

thisdict.clear()
print(thisdict)

 

O/p:
 {}

 

 

5. Dictionary Length:-

To check how many items are there in dictionary by using the len() method.

e.g:-

   thisdict = {
  "brand": "honda",
  "model": "shine",
  "year": 2019
}
 print(len(thisdict))

O/p:-
3
6. Return the values:-
The method values () returns a view object.
e.g:-

thisdict = {
  "brand": "honda",
  "model": "shine",
  "year": 2019
}
x=thisdict.values
print(x)

O/p:-
Honda
Shine
2019

7. setdefault the values:-

The setdefault() method return item with specified key. If the key does not exit etc.

e.g:-

thisdict = {
  "brand": "honda",
  "model": "shine",
  "year": 2019
}

x = car.setdefault("model", "Splender")

print(x)

O/p:-
Splender

 

Loop through dictionary:-

You can also use loop through dictionary by using the for loop.
While looping the return value are keys of dictionary but method returns the values as well.
e.g:-
thisdict = {
  "brand": "honda",
  "model": "shine",
  "year": 2019
}

for x in thisdict:
  print(x)

O/p:-
Honda
shine
2019

Additional Services : Refurbished Laptops Sales, Python Classes, Share Market Classes And SEO Freelancer in Pune, India