World's Best AI Learning Platform with profoundly Demanding Certification Programs
Designed by IITians, only for AI Learners.
Designed by IITians, only for AI Learners.
New to InsideAIML? Create an account
Employer? Create an account
Download our e-book of Introduction To Python
4.5 (1,292 Ratings)
559 Learners
Pallavi Dhotre
2 years ago
# python dictionary example
demo_dict={} #creating empty dictionary
demo_dict={'name':'Mariya','age':25,'Country':'India'} #create dictionary in python 3
print(demo_dict)
{'name': 'Mariya', 'age': 25, 'Country': 'India'}
# python dictionary example
demo_dict={'name':'Mariya','age':25,'Country':'India'} #create dictionary in python 3
# accessing the elements of dictionary using python dictionary keys
print(demo_dict['name']) # name is a key and Mariya is a value
print(demo_dict['age']) # age is a key and 25 is a value
print(demo_dict['Country']) # Country is a key and India is a value
Mariya
25
India
demo_dict={'name':'Mariya','age':25,'Country':'India'} #create dictionary in python 3
print(demo_dict) #before clear() method
demo_dict.clear() #using clear() method
print(demo_dict) #after using clear() method
{'name': 'Mariya', 'age': 25, 'Country': 'India'}
{}
demo_dict={'name':'Mariya','age':25,'Country':'India'} #create dictionary in python 3
x=demo_dict.copy() #creating copy of the dictionary
print(x)
{'name': 'Mariya', 'age': 25, 'Country': 'India'}
demo_dict={'name':'Mariya','age':25,'Country':'India','Hobby':'Sports'} #create dictionary in python 3
x=demo_dict.fromkeys(['age'],25) #accessing specific element
print(x)
{'age': 25}
demo_dict={'name':'Mariya','age':25,'Country':'India','Hobby':'Sports'} #create dictionary in python 3
x=demo_dict.get('name') #accessing specific element
print(x)
Mariya
demo_dict={'name':'Mariya','age':25,'Country':'India','Hobby':'Sports'} #create dictionary
x=demo_dict.items() #view the elements in dictionary as a tuple
print(x)
dict_items([('name', 'Mariya'), ('age', 25), ('Country', 'India'), ('Hobby', 'Sports')])
demo_dict={'name':'Mariya','age':25,'Country':'India','Hobby':'Sports'} #create dictionary
print(demo_dict) # before removing the item
demo_dict.pop('Hobby')
print(demo_dict) #after removing the item
{'name': 'Mariya', 'age': 25, 'Country': 'India', 'Hobby': 'Sports'}
{'name': 'Mariya', 'age': 25, 'Country': 'India'}
demo_dict={'name':'Mariya','age':25,'Country':'India','Hobby':'Sports'} #create dictionary
print(demo_dict) # before removing the item
demo_dict.popitem()
print(demo_dict) #after removing the item
{'name': 'Mariya', 'age': 25, 'Country': 'India', 'Hobby': 'Sports'}
{'name': 'Mariya', 'age': 25, 'Country': 'India'}
demo_dict={'name':'Mariya','age':25,'Country':'India','Hobby':'Sports'} #create dictionary
print(demo_dict.keys()) #return keys
dict_keys(['name', 'age', 'Country', 'Hobby'])
demo_dict={'name':'Mariya','age':25,'Country':'India','Hobby':'Sports'} #create dictionary
print(demo_dict.values()) #return values
dict_values(['Mariya', 25, 'India', 'Sports'])
demo_dict={'name':'Mariya','age':25,'Country':'India','Hobby':'Sports'} #create dictionary
print(demo_dict) #without update()
demo_dict.update({'1':111})
print(demo_dict) #after update()
{'name': 'Mariya', 'age': 25, 'Country': 'India', 'Hobby': 'Sports'}
{'name': 'Mariya', 'age': 25, 'Country': 'India', 'Hobby': 'Sports', '1': 111}
demo_list=[{'name':'Mariya','age':25},{'Country':'India','Hobby':'Sports'}] #create a list of dictionaries python
print(demo_list)
#access the elements from the list of dictionary
print(demo_list[0]['age']) # this will return 25
print(demo_list[1]['Country']) # this will return India
[{'name': 'Mariya', 'age': 25}, {'Country': 'India', 'Hobby': 'Sports'}]
25
India
books=['history','maths','science'] #create a list
#first approach python create dictionary from list
books_dict=dict.fromkeys(books,'available') #create dictionary from list using fromkeys() method
print(books_dict)
#second approach python create dictionary from list
books_dict1={books:'should study'for books in books} #create dictionary from list using dictionary comprehension
print(books_dict1)
#third approach python create dictionary from list
price=[250,450,670]
books_dict2=dict(zip(books,price)) #create dictionary from list using zip()
print(books_dict2)
{'history': 'available', 'maths': 'available', 'science': 'available'}
{'history': 'should study', 'maths': 'should study', 'science': 'should study'}
{'history': 250, 'maths': 450, 'science': 670}