Introduction-to-programming

A single repo for introduction to programming in X

View on GitHub

Dictionaries

Introduction

A dictionary is used to store a key value pair. A key is a variable which is assigned a value in the dictionary.

The keys are unique in the dictionary while a value may not be. Values can be on any type but the keys need to be immutable.

Syntax

Each key is separated from its value by a colon (:), the items are separated by commas, and the whole thing is enclosed in curly braces. An empty dictionary without any items is written with just two curly braces, like this: {}.

Initialize:

Start a new dictionary with some elements and access them.

love = {'Sam': 'Gilly', 'Tormund': 'Brienne', 'Jaime': 'Cersei', 'Hound': 'Chicken'}
#Access
print(love['Sam']) #Gilly
print(love['Hound']) #Chicken

Update Dictonary:

Add new entries to a dictionary, delete an entry and clear and delete the entire dictionary.

#Take the dictionary declared above.

#Add new entry
love['Jorah'] = 'Daenerys'
love['Ramsay'] = 'Sansa' #OOPS..

#Remove an entry
del love['Ramsay']
print(love['Ramsay']) #Gives Error

#lets remove everything
love.clear()
print(love['Tormund']) #Gives Error that Tormund is not a key

#lets delete love
del love
print(love['Jaime']) #Gives Error that love is not defined

Properties

Keys are unique:

Really?? Just jump to the code.

#Consider love

#Lets use same key twice
love['Littlefinger'] = 'Catelyn'
love['Littlefinger'] = 'Sansa'
print(love['Littlefinger']) #Sansa

Yes, the latest value gets assigned.

Keys must be immutable:

The following are some immutable objects:

  • int
  • float
  • decimal
  • complex
  • bool
  • string
  • tuple
  • range
  • frozenset
  • bytes

Functions

These are called directly and the dictionary object is passed as (function(dict))

len()

Gives the total length of the dictionary. This would be equal to the number of items in the dictionary.

dict = {'Key': 'Value'}
print(len(dict)) #1

str()

Gives a string representation of the dictionary.

#Consider love
print(str(love)) #Try yourself

Methods

These are called using the dictionary object like (dict.method())

clear()

Removes all elements of dictionary.

copy()

Returns a copy of the dictionary, which can be stored in another dictionary.

dict1 = {'Key': 'Value'}
dict2 = dict1.copy() #Now dict2 has all elements of dict1

get()

It returns the value of the key or None if not found. You can also set a default parameter that will be returned if the key is not found.

#consider love
print(love.get('Jaime')) #Cersei
print(love.get('Bronn')) #None
print(love.get('Bronn', 'You wouldn\'t know her')) #You wouldn't know her

keys()

It gives a list of all keys of the dictionary.

print(str(love.keys())) #Try yourself

values()

It gives a list of all values of the dictionary.

print(str(love.values())) #Try yourself

More

Explore other functions of dictionary here