Dictionary and APIs AttainU

 Dictionary and APIs

 

Dictionary also called map or hash.

Dictionary use curly braces {}.

 

 
   dict = {

"key":"value1",
"key2": "value2",
"key3": "value3",

}

print(type(dict))
print(dict["key3"])


    output: <class 'dict'>
            value3


 

 another way to code dictionary.

 
dict1 = {

0:"value1",
1: "value2",
2: "value3",

}

print(type(dict1))
print(dict1[2])
 
     
    output: <class 'dict'>
            value3



 

indexing into dictionary


d = {

"key":25,
"key2": "Hyderabad",
"key3": [8,7,4,1,2,3,5,6],

}

print(type(d))
print(d["key3"][4])
print(d["key2"][0:6])
 
 
      output:    
        <class 'dict'>
        2
        Hydera


 

update dictionary value "Jazib"


homeTown_students = {

"sudhakar": "Bhubanesawr",
"Aniket": "Gwalior",
"Brian": "Mangalore",
"Imran": "Delhi",
"Jazib": "Gorakhpur",
}
 
 
print(homeTown_students["Jazib"])

homeTown_students["Jazib"] = "Lucknow"

print(homeTown_students)
 
 
     output:
 
        Gorakhpur 

        {'sudhakar': 'Bhubanesawr', 'Aniket': 'Gwalior', 
        'Brian': 'Mangalore', 'Imran': 'Delhi', 'Jazib': 'Lucknow'}
 
 
 

 


Remove elements from Dictionary

 
 homeTown_students = {

"sudhakar": "Bhubanesawr",
"Aniket": "Gwalior",
"Brian": "Mangalore",
"Imran": "Delhi",
"Jazib": "Gorakhpur",
}
 
    
## Remove elements from dictionary
homeTown_students.pop("Aniket")
print(homeTown_students)
 
 

   output:
 
        {'sudhakar': 'Bhubanesawr', 'Brian': 'Mangalore', 
        'Imran': 'Delhi', 'Jazib': 'Gorakhpur'}
 
 

 

 insert elements in dictionary

 
 
homeTown_students = {

"sudhakar": "Bhubanesawr",
"Aniket": "Gwalior",
"Brian": "Mangalore",
"Imran": "Delhi",
"Jazib": "Gorakhpur",
}
 
 
 # inserting dictionary
homeTown_students["cyed"] = "Northeast"
print(homeTown_students)
 
     output:
 
    {'sudhakar': 'Bhubanesawr', 'Aniket': 'Gwalior', 'Brian': 'Mangalore', 
        'Imran': 'Delhi', 'Jazib': 'Gorakhpur', 'cyed': 'Northeast'}


 

 

Print keys from the dictionary

 
 
   homeTown_students = {

"sudhakar": "Bhubanesawr",
"Aniket": "Gwalior",
"Brian": "Mangalore",
"Imran": "Delhi",
"Jazib": "Gorakhpur",
}
 
    print(homeTown_students.keys())
 
    output:
 
     dict_keys(['sudhakar', 'Aniket', 'Brian', 'Imran', 'Jazib'])
 
 

 

 

Print Values from the dictionary  

 
  homeTown_students = {

"sudhakar": "Bhubanesawr",
"Aniket": "Gwalior",
"Brian": "Mangalore",
"Imran": "Delhi",
"Jazib": "Gorakhpur",
}
 
     

## print only values from dictionary
print(homeTown_students.values())
 
 
 
  output:
 
   dict_values(['Bhubanesawr', 'Gwalior', 'Mangalore', 'Delhi', 'Gorakhpur'])
 
 

 

 

 

 

for loop and api 


 
homeTown_students = {

"sudhakar": "Bhubanesawr",
"Aniket": "Gwalior",
"Brian": "Mangalore",
"Imran": "Delhi",
"Jazib": "Gorakhpur",
}
 

     for k in homeTown_students.keys():
print(k, "'s home town is",homeTown_students[k])
 
 
 
    output: 
     
    sudhakar 's home town is Bhubanesawr
    Aniket 's home town is Gwalior
    Brian 's home town is Mangalore
    Imran 's home town is Delhi
    Jazib 's home town is Gorakhpur
 
 

 

convert dictionary into list

 


homeTown_students = {

"sudhakar": "Bhubanesawr",
"Aniket": "Gwalior",
"Brian": "Mangalore",
"Imran": "Delhi",
"Jazib": "Gorakhpur",
}

## convert dictionary into list
print(list(homeTown_students.keys())[0])
 
 
    output: sudhakar


 

 

Find the maximum value in python

 
 # find the maximum value

num = [1,2,3,4,50,6,7]

max = float("-inf") # this is minus infinity

for i in num:
if i > max:
max = i
print(max)
    
    output: 50

 

 

 

 

 

 

 

 

إرسال تعليق (0)
أحدث أقدم