__Dictionary in python __

Dictionaries in python are unordered collection of items,a dictionary is closed by curly brackets. Dictionary hold pair of **Key** and **Value** (key**:**value). Key and value are separated by (:) , whereas each item in dictionary is separated by comma (,).

**Some Functions and Methods for dictionary** (dict-any dictionary)

Function and Methods | Description |

cmp(d1,d2) | Compares elements of both dict. |

len(dict) | Gives the total length of the dictionary.(items) |

str(dict) | Produces a printable string representation of a dictionary |

type(variable) | Returns the type of the passed variable. |

dict.clear() | Removes all elements of dictionary dict |

dict.copy() | Returns a shallow copy of dictionary dict |

dictformkeys() | Create a new dictionary with keys from seq and values set to value. |

dict.get() | For key key, returns value or default if key not in dictionary |

dict.items() | Returns a list of dict's (key, value) tuple pairs |

dict.key() | Returns list of dictionary dict's keys |

dict.setdeafult() | Similar to get(), but will set dict[key]=default if key is not already in dict |

dict.update() | Adds dictionary in other dict. |

dict.values() | Returns list of dictionary dict's values |

**Example** ( __Creating Dictionary__) -Different types to create dictionary

#empty dictionary

d={}

print(d)

#with integer key

d1={1:'a',2:'b',3:'c'}

print(d1)

#with mixed key

d2={'Name':'Goedhub','L':[1,2,3,3]}

print(d2)

#using dict() method

d3=dict({1:'a',2:'c'})

print(d3)

#using items in pair

d4=dict([(1,'a') (2,'c')])

print(d4)

Output

{}

{1: 'a', 2: 'b', 3: 'c'}

{'Name': 'Goedhub', 'L': [1, 2, 3, 3]}

{1: 'a', 2: 'c'}

{1:'a',2:'c'}

**Example **(__Accessing elements in dictionary and for loop in dictionary __)

d = {"Johnny": "5 years old", "Sally": "7 years old","Eva":"10 years old","Peggy": "7 years old"}

for i,j in d.items():

print(i, "-", j)

Note:- Here i and j represent Keys and Values

Output

Johnny - 5 years old

Sally - 7 years old

Eva - 10 years old

Peggy - 7 years old

**Example**

for i in d.items():

print(i)

for i in d:

print(i)

Note:-Here you can see importance of d.items() Method

Output

('Johnny', '5 years old')

('Sally', '7 years old')

('Eva', '10 years old')

('Peggy', '7 years old')

('Ted', '5 years old')

Johnny

Sally

Eva

Peggy

**Example** (For key and value function)

for i in d.items():

#print(i)

print(i[0], "-", i[1])

for i in d.keys():

print(i)

for value in d.values():

print(value)

Output

Johnny - 5 years old

Sally - 7 years old

Eva - 10 years old

Peggy - 7 years old

Johnny

Sally

Eva

Peggy

5 years old

7 years old

10 years old

7 years old

**Example (Adding items in dictionary )**

d['Ted'] = "5 years old"

print(d)

Output

{'Johnny': '5 years old', 'Sally': '7 years old', 'Eva': '10 years old', 'Peggy': '7 years old', 'Ted': '5 years old'}

**Removing an item in dictionary** - For deleting a specific value we use pop() and popitems() functions. "del dict_name" is used to delete entire dictionary.

**Example**

d1={1:'a',2:'b',3:'c'}

print(d1.pop(2))

del d1[1]

print(d1)

del d1

print(d1)

Output

b

{3: 'c'}

NameError Traceback (most recent call last) <ipython-input-96-b5c7f4fcce27> in <module> 1 del d1 ----> 2 print(d1) NameError: name 'd1' is not defined

Error cause- d1 (dictionary) is deleted already

**Python Tutorial **

**Machine Learning Tutorial **

**AI Tutorial**

**Free Online Tutorials**