Python | Dictionary

A dictionary is an associative array (also known as hashes).

Any key of the dictionary is associated with a value. The value of a dictionary

can be any Python data type. So dictionaries are unordered key-value.


A dictionary is a collection that is unordered, changeable, and indexed.


In python, the dictionary is written with curly brackets and they have keys

and values.


Dictionaries are mutable, which means they can be changed.

Example: x = ['rohit',20['chichore']]

Why we use Dictionaries?

Because of the limitation of lists, lists are not enough to represent real-world data.

How to create dictionaries?


User= {'name':,'rohit','age':20}
print(x)
print(type(User))
Output:
{'name':'rohit','age',20}
output: <class 'dict'>

How to access data in a dictionary?


User= {'name':'rohit','age':20}
print(User['name'])
print(User['age'])
Output:
rohit 
20

Which type of data a dictionary can store?

Anything number, string, list, dictionary, etc

User_info={
          'name':'rohit'
          'age' : 20
          'fav_food': []
        }
User_info =
 {
           'name' : 'rohit'
           'age' : 20
           'fav_movie' : ['chichore']
  }
print(user_info['fav_movie'])
Output:
['chichore']

How to add data to an empty dictionary?


User_info2={}
User_info2['name']='rahul'
print(user_info2)
Output:
{'name':'rahul'}
Use_info2['age']=21
print(User_info2)
Output: 
{'name':'rahul','age':21}

Happy Coding!

Follow us on Instagram @programmersdoor

Join us on Telegram @programmersdoor

Please write comments if you find any bug in the above code/algorithm, or find other ways to solve the same problem.

Follow Programmers Door for more.

#blog #interview #placement #learn #computer #science

        Contact Us

programmersdoor@gmail.com

  • LinkedIn
  • Facebook
  • Instagram

©2023 by Programmers Door