Dictionary functions and methods in Python

 


Function of Dictionary

Get()

This function get the value from dictionary with using key

Example

Get the value of “Brand” key

d = {
  
"Brand""Honda",
  
"Model""City",
  
"year"1970,
}

x = d.get(“Brand”)

print(x)

Output:

Honda

Keys()

It is return the keys form dictionary

Example

Get the keys from a dictionary using “for loop”

d = {
  
"Brand""Honda",
  
"Model""City",
  
"year"1970,
}

for x in d.keys():

print(x)

Output:

Brand

Model

Year

values()

This function get the all values from dictionary

Example

Get the value from a dictionary using “for loop”

d = {
  
"Brand""Honda",
  
"Model""City",
  
"year"1970,
}

for x in d.values():

print(x)

Output:

Honda

City

1970

Items()

This function get the all keys and values from dictionary

Example

Get the keys and value from a dictionary using “for loop”

d = {
  
"Brand""Honda",
  
"Model""City",
  
"year"1970,
}

for x,y in d.items():

print(x,y)

Output:

Brand    Honda

Model   City

Year       1970

del()

del() is keyword, with this we can delete any data from dictionary using key.

Example

Delete data “year:1970” from dictionary

d = {
  
"Brand""Honda",
  
"Model""City",
  
"year"1970,
}

del d [“Year”]

print(d)

Output:

{‘Brand’:‘Honda’, ‘Model’: ‘City’}

pop()

pop() also work as del() but its define another type

Example

Delete data “year:1970” from dictionary

d = {
  
"Brand""Honda",
  
"Model""City",
  
"year"1970,
}

d.pop(“Year”)

print(d)

Output:

{‘Brand’:‘Honda’, ‘Model’: ‘City’}

Note: It display deleted data also

print (d.pop(“Year”))

Output:

1970

dict()

With this function we can create a new dictionary and we can enter multiple parameters.

Example

Create a new dictionary

d = dict(Brand="Honda",Model="City",year=1970)
print(d)

Output:

{"Brand""Honda","Model""City","year"1970}

update()

through this function we can update any data in dictionary

Example

Update data “Model:City” from dictionary

d = {
  
"Brand""Honda",
  
"Model""City",
  
"year"1970,
}

d.update({“Model”:”Civic”})

print(d)

Output:

{‘Brand’:‘Honda’, ‘Model’: ‘Civic’, ‘Year’:’1970’}

clear()

Using clear() function clear all data from dictionary.

Example

d = {
  
"Brand""Honda",
  
"Model""City",
  
"year"1970,
}

d.clear()

print(d)

Output:

{ }

insert()

it is used to Insert a new data in dictionary

Example

Insert new data “color:red” in dictionary

d = {
  
"Brand""Honda",
  
"Model""City",
  
"Year"1970,
}

d [“Color”] = “Red”

print(d)

Output:

{‘Brand’:‘Honda’, ‘Model’: ‘City’, ‘Year’:’1970’, ‘Color’:’Red’}

Note: If the new key is already in dictionary then exists data will be updated.

Example

d [“Year”] = “2000”

print(d)

Output:

{‘Brand’:‘Honda’, ‘Model’: ‘City’, ‘Year’:’2000’}

<<Previous                                                  Next>>

कोई टिप्पणी नहीं

टिप्पणी: केवल इस ब्लॉग का सदस्य टिप्पणी भेज सकता है.

Send Multiple Emails From Excel | Send Bulk Mail from Excel Sheet with Attachment in One Click

Send Multiple Emails From Excel | Send Bulk Mail from Excel Sheet with Attachment in One Click Download VBA Code Notepad file - Click here D...

Blogger द्वारा संचालित.