FIFA-2022 Career Guide Free Tutorials Go to Your University Placement Preparation 
2 like 0 dislike
4.0k views
in Python Programming by Goeduhub's Expert (3.1k points)
edited by

Dictionaries in python is an important and basic concept to understand and to make your basics strong. Question such as ; What is dictionaries in python ? How to define a dictionary ? How to access elements in dictionary and how to delete a dictionary ? discussed in this article. 

Related Links: Basic Python 

by (112 points)
reshown by
date:23/4/2020

In todays session i  learn what is  tuple ,dictionary ,sets,function in python. where it's use?

and how to its use in python programming
by (100 points)
Dictionaries is one of the features of python i love the most.

4 Answers

2 like 0 dislike
by Goeduhub's Expert (3.1k points)
edited by
 
Best answer

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

by (454 points)
what is the difference between del and pop() in dictionary?
by (422 points)
Dictionary is unordered list and important topic in python to learn.
by Goeduhub's Expert (2.2k points)
Difference between del and pop() in dictionary-
d = {"A": 123, "B": 456, "C": 789}
#remove via pop
d.pop("X")

#remove via del
del d["X"]

Use d.pop if you want to capture the removed item, like in item = d.pop("A"). It returns the value of deleted key.
Use del if you want to delete an item from a dictionary.

Use del if you want to delete an item from a dictionary.
by (294 points)
I this artical I learned that how to use dictionary in python .and how to access
by (102 points)
Date:25/04/2020
Topic:Dictionary
Learned about Dictionary .got to know how to create dictionary and how to delete add element in dictionary
by (135 points)
dictionary(key:value) -creating dictionary
by (262 points)
how to use python dictionaries to read the tabular data ??
by (100 points)
In this article, i learned about dictionaries. These are the unordered collection of items. These are alike associative arrays. It collects an element has teo attributes key and values.
by (471 points)
Del keyword delete whole dictionary while pop delete only last element of dictionary.
by (279 points)
Date-07-05-2020
Today we learn about set,dictionary and tuple and some interesting facts about seaborn iibrary.
0 like 0 dislike
by (391 points)
Date: 23/04/2020

Topic: Python Dictionaries

Today i have learned that python dictionaries has key and values. Got to know some of the functions and how to create a dictionary. How to add, removes items and accessing elements in dictionaries.
0 like 0 dislike
by (112 points)
date:23/4/2020

In todays session i will learn what is  tuple ,dictionary ,sets,function in python. where it's use?

and how to its use in python programming
0 like 0 dislike
by (294 points)
In this I learned about the dictionary and how to its implementation in python programming

Learn & Improve In-Demand Data Skills Online in this Summer With  These High Quality Courses[Recommended by GOEDUHUB]:-

Best Data Science Online Courses[Lists] on:-

Claim your 10 Days FREE Trial for Pluralsight.

Best Data Science Courses on Datacamp
Best Data Science Courses on Coursera
Best Data Science Courses on Udemy
Best Data Science Courses on Pluralsight
Best Data Science Courses & Microdegrees on Udacity
Best Artificial Intelligence[AI] Courses on Coursera
Best Machine Learning[ML] Courses on Coursera
Best Python Programming Courses on Coursera
Best Artificial Intelligence[AI] Courses on Udemy
Best Python Programming Courses on Udemy

Related questions

0 like 0 dislike
5 answers 2.5k views
0 like 0 dislike
4 answers 2.5k views
2 like 0 dislike
7 answers 10.1k views
1 like 0 dislike
3 answers 2.8k views
0 like 0 dislike
3 answers 3.0k views

 Important Lists:

Important Lists, Exams & Cutoffs Exams after Graduation PSUs

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy ||  Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 

 

Free Online Directory

...