WEB DEVELOPER SITE
PHP7
 

Python Dictionary

The value of dictionary can be any type but key must be data type string

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: {}.


Python dictionary are written as curly brackets.

Value in Dictionary

you can use the familiar square brackets along with the key to obtain its value.

Example

There are following example of Value in Dictionary: -
thisdict =	{
  "Mohan": "5",
  "Sara": "7",
  "Zara": "8"
}
print(thisdict)
Result
{'Mohan': '5', 'Sara': '7', 'Zara': '8'} 

Run Example »

Change Values

You can change the value of a specific item by referring to its key name:

Example

thisdict =	{
  "Job": "Software",
  "work": "python",
  "year": 2014
}
thisdict["year"] = 2018

Run Example »

Delete Dictionary Elements

Clear the entire contents of a dictionary.

dict = {'Name': 'Sara', 'Age': 8, 'Class': 'First'}
del dict['Name']; # remove entry with key 'Name'
dict.clear();     # remove all entries in dict
del dict ;        # delete entire dictionary

print "dict['Age']: ", dict['Age']
print "dict['School']: ", dict['School']
Answer :
dict['Age']:
Traceback (most recent call last):
   File "test.py", line 8, in 
      print "dict['Age']: ", dict['Age'];
TypeError: 'type' object is unsubscriptable
Note − del() method is discussed in subsequent section. Run Example »

Removing Items

There are several methods to remove items from a dictionary:

The pop() method removes the item with the specified key name:

Example

thisdict =	{
  "Job": "Software",
  "work": "python",
  "year": 2014
}
thisdict.pop("Job")
print(thisdict)



Run Example »

The del keyword removes the item with the specified key name:

Example

thisdict =	{
  "Job": "Software",
  "work": "python",
  "year": 2014
}
del thisdict["year"]
print(thisdict)


Run Example »

The clear() keyword empties the dictionary:

Example

thisdict =	{
  "Job": "Software",
  "work": "python",
  "year": 2014
}
thisdict.clear()
print(thisdict)
Run Example »

The dict() Constructor

it is also possible to use the dict() constructor to make a dictionary:
thisdict = dict(Job="Software", work="python", year=2014)
# note that keywords are not string literals
# note the use of equals rather than colon for the assignment
print(thisdict)

Run Example »

Built-in Dictionary Methods

Python has a set of built-in methods that you can use on dictionaries.

Method Description
clear()Removes all the elements from the dictionary
copy()Returns a copy of the dictionary
fromkeys()Returns a dictionary with the specified keys and values
get()Returns the value of the specified key
items()Returns a list containing the a tuple for each key value pair
keys()Returns a list contianing the dictionary's keys
pop()Removes the element with the specified key
popitem()Removes the last inserted key-value pair
setdefault()Returns the value of the specified key.
If the key does not exist: insert the key, with the specified value
update()Updates the dictionary with the specified key-value pairs