Skip to content
cs2study

cs2study

Computer Science C.B.S.E syllabus

  • Home
  • Python-Class XI
    • Videos
    • PDF/PPT
    • Programs
    • Papers
    • MCQ Quiz
    • Project/Practical File Class XI
    • Online Class-XI
  • Python-Class XII
    • Videos
      • File Handling
      • Python Functions
      • Stack
      • MySQL with Python
      • Videos-Networking
    • PDF/PPT
      • Function
      • Python Modules
      • File Handling
      • Stack and Queue
      • MySQL
      • Networking
    • Programs
      • Function Programs
      • File Handling
      • Stack and Queue
      • MySQL
      • MySQL Connectivity
    • Papers
      • Sample Papers
      • School Papers
      • Assignment/Test
    • MCQ
      • MCQ Quiz
      • MCQ Chapter-wise
    • Projects & Practical Files
    • Board Practical Exam
  • Python-Quick Revision
    • Notes
    • Errors and Outputs
    • Exam Revision Notes
    • Question Bank
  • C++
    • Notes
    • Programs/Questions
    • Project/Practical File C++
  • MySQL Software
  • Syllabus
  • Toggle search form

Dictionary

Posted on By Pallavi S 19 Comments on Dictionary

Dictionary

    • Unordered collection of items / elements. Each item has two parts – key : value
       A mapping between key and it’s value, separated by colon (:)
       Items are enclosed in {} and separated by comma.
       Are optimized to retrieve data using key. So key (values) should be unique. They
      can be integer, float, string or tuple.
       D[key] can be used for accessing element, also to add element in an existing
      dictionary.


      Creating dictionary

       Using assignment operator creates deep copy
       Using function
      1. dict()
      2. copy() creates shallow copy
      3. eval()
       {} to create blank dictionary


      Dictionary method / function

       As key value is used to access elements, trying to retrieve an non existing key will
      result into error
       For adding elements in existing dictionary
                           dictionaryName[newKey] = value
                            If newKey matches to an existing key, then value gets updated.
       Del statement can be used to delete an element or entire dictionary
       len(), clear() method works for dictionary also
       keys() method can be used to get all key values existing the dictionary
       values() method can be used to get all values present in dictionary
       Items() method returns all key: value pairs as tuple. Which can then be used in
      for loop for processing

D = {1:’one’,2:’two’}
for i,j in D.items():
print(i,j)

 Converting a dictionary to list gives you a list of key values only.
 Get(key) method returns value mapped to the key
 Update(dictionary) method adds the elements of dictionary passed as argument
to existing dictionary
 pop(key) method removes the key:value mapping and returns the value removed
 popitem() method removes an arbitrary pair from dictionary


Function/Method Purpose Example

len()- This function returns the number of elements i.e.the key-value pairs present in the dictionary.

>>>d={1: ‘Amrit’, 2: ‘Bhavesh’, 3: ‘Chetan’, 4: ‘Falguni’}
>>> len(d)
4


d.items()- This function returns the list of elements in the dictionary.

>> d.items()
dict_items([(1, ‘Amrit’), (2,’Bhavesh’), (3, ‘Chetan’),(4, ‘Falguni’)])


d.keys()- This function returns the all the keys that are present in the dictionary.

>> d.keys()
dict_keys([1, 2, 3, 4])


d.values()- This returns all the values in the dictionary.

>> d.values()
dict_values([‘Amrit’,’Bhavesh’, ‘Chetan’,’Falguni’])


d.get(key) -This function returns the value corresponding to a key in the dictionary. If the key is not present,

>>> d.get(2)
‘Bhavesh’

the function returns,
‘None’.

>> d.get(8, -1)
-1


d.update(object)- This function merges the object enclosed in brackets with d.

>> d2={5: ‘Kartikay’, 6:
‘Prerna’}
>>> d.update(d2)
>>> d
{1: ‘Amrit’, 2: ‘Bhavesh’, 3:’Chetan’, 4: ‘Falguni’, 5:’Kartikay’, 6: ‘Prerna’}


d.clear() -This function clears the entire dictionary. It deletes all the key-value pairs.

>>d2={1:”Deer”,2:”Bear”, 3:”Cat”,4:”Elephant”}
>>>d2
{1:”Deer”, 2:”Bear”,3:”Cat”, 4:”Elephant”}
>>>d2.clear()
>>>d2
{}


d.pop(key)- This function removes a key along with its value in a dictionary.

>> d2={1:”Apple”,2:”Ball”,3:”Pineapple”,4:”Mangoes”}
>>> d2.pop(2)
‘Ball’
>>> d2
{1: ‘Apple’, 3: ‘Pineapple’,4: ‘Mangoes’}


 

 

 

 

 

 

 

Class XI, Class XI

Post navigation

Previous Post: MySQL Python Connectivity Commands
Next Post: LIST

Comments (19) on “Dictionary”

  1. Carter4951 says:
    at

    Monetize your traffic instantly—enroll in our affiliate network! https://shorturl.fm/iLAL2

  2. Greta1705 says:
    at

    Become our partner and turn clicks into cash—join the affiliate program today! https://shorturl.fm/JcxVX

  3. Aspen4557 says:
    at

    Become our affiliate and watch your wallet grow—apply now! https://shorturl.fm/N68W4

  4. Clarissa1282 says:
    at

    Earn passive income this month—become an affiliate partner and get paid! https://shorturl.fm/YfJxN

  5. Gabriel4889 says:
    at

    Turn traffic into cash—apply to our affiliate program today! https://shorturl.fm/iASoL

  6. Alexandria1362 says:
    at

    Turn your network into income—apply to our affiliate program! https://shorturl.fm/QDnl5

  7. Haven2614 says:
    at

    Share our link, earn real money—signup for our affiliate program! https://shorturl.fm/FLtL6

  8. Blake4324 says:
    at

    https://shorturl.fm/4kn4h

  9. https://www.fapjunk.com says:
    at

    I do not even know the way I finished up here, but I believed
    this submit was once good. I don’t realize who you are but definitely you’re goking to a famous blogger should you
    are not already. Cheers!

    Herre is my blog … https://www.fapjunk.com

  10. Jonas3376 says:
    at

    https://shorturl.fm/kPHzC

  11. Vanessa3958 says:
    at

    https://shorturl.fm/msyDn

  12. https://www.letmejerk.com says:
    at

    Magnificent goods from уou, man. I һave keep in minmd уoսr stuff ρrevious tto and уⲟu arе jut extremely magnificent.
    І actually like ԝhat yօu hаvе obtained гight here,
    really like whnat you are saying and thе Ƅeѕt
    waay during which yoս sayy it. Yoou make it enjoyable
    ɑnd yoս ѕtіll care for to keеp it sensіble.

    I can’t wait to learn fɑr more from yoս. This is really
    a wonderful site.

    Hɑve a ⅼook ɑt my website :: https://www.letmejerk.com

  13. omegle alternative says:
    at

    Kеep this going ρlease, great job!

    Alsoo visit my blog; omegle alternative

  14. Dominic2263 says:
    at

    https://shorturl.fm/1q6li

  15. Corey3396 says:
    at

    https://shorturl.fm/4Hiud

  16. Jesse2875 says:
    at

    https://shorturl.fm/sd1ue

  17. Christopher2410 says:
    at

    https://shorturl.fm/KJACh

  18. Alfred291 says:
    at

    https://shorturl.fm/MjslR

  19. Lilian3805 says:
    at

    https://shorturl.fm/SOuXE

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Tips-Networking Case-based Questions
  • XII-Board Practical Exam 2020-21
  • Stack and Queue
  • Questions on Tuples
  • Networking -Full Forms

Recent Comments

  • vorbelutr ioperbir on Stack and Queue
  • vorbelutr ioperbir on LOOPS
  • vorbelutr ioperbir on MySQL-Constraints
  • vorbelutrioperbir on LIST
  • купить дрова в подольске_oaPn on Tips-Networking Case-based Questions

Categories

  • About
  • C++
    • Notes
    • Programs/Questions
    • Project/Practical File C++
  • Class XI
    • MCQ
    • Online Class-XI
    • PDF/PPT
  • Class XII
    • Assignment/Test
    • Board Practical Exam
    • File Handling
    • File Handling
    • Function Programs
    • Notes
    • Projects and Practical Files Class XII
      • Project/Practical File Class XI
      • Stack
        • Stack and Queue
    • Quick Revision
      • Errors and Outputs
      • Exam Revision Notes
      • Question Bank
    • Stack and Queue
  • File Handling
  • Function
  • Home
  • MCQ Chapter-wise
  • MCQ Quiz
  • MCQ Quiz
  • MySQL
  • MySQL Software
  • Papers
  • Papers
  • PDF/PPT
    • MySQL
    • MySQL with Python
    • Networking
  • Practical/Project
  • Programs
  • Programs
    • MySQL Connectivity
  • Python Functions
  • Python Modules
  • Python_fundamentals
  • Sample Papers
  • School Papers
  • Syllabus
  • Videos
  • Videos
  • Videos-Networking

Archives

  • March 2021
  • February 2021

Categories

  • Assignment/Test paper
  • Board Practical Exam
  • Class XI
  • Class XI
  • Class XII
  • Function
  • MySQL Notes
  • MySQL with Python
  • Networking
  • tuple
  • XI Videos

Copyright © 2025 cs2study.

Powered by PressBook WordPress theme