Python Dictionaries

We have seen how lists work in Python. A dictionary is like a list. But a list just contains values but a dictionary associates with each key a value. Just like how in a dictionary , each word maps to a particular meaning, in Python , a dictionary has keys and value associated with each key. Dictionaries are mutable. That means that they can be changed.

  • We create a dictionary using curly braces{}. The keys are separated using commas and there exists a : between the key and its corresponding value. Let us see an example. Observe the ‘ : ‘ and the ‘ , ‘.screenshot-71.png 
  • Let us see how to change a value of a particular key.screenshot-722.png
  • Let us see how to add a key and value to a dictionary.screenshot-732.png
  • Let us see how to delete a particular key.screenshot-74.png
  • Let us print the content of a dictionary by iteration using a for loop.screenshot-751.pngscreenshot-75.png
  • Let us find the length of the dictionary.screenshot-76.png
  • Let us print the dictionary after sorting it. The values of the key are sorted.screenshot-77.png
  • Let us check if particular keys are present in the dictionary or not.screenshot-78.png

Image Courtesy: Google Images

 

          

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Powered by WordPress.com.

Up ↑

%d bloggers like this: