Skip to main content

Python Accessing elements of dictionary-all keys() all values() all items()

Python Accessing elements of dictionary-all keys() all values() all items() 

#Accessing elements of dictionary

emp={"empname":"krishna","jobdesc":"software programmer","salary":34000}

print(emp['empname'])    #access value using name

print(emp.get('salary')) #get key value using key name in get function

#get all keys and values

#keys(): return the list of all keys in present dictionary

#ex:

emp={"empname":"krishna","jobdesc":"software programmer","salary":34000}

print(emp.keys())

print(type(emp.keys()))


#values():return the list of all values in present dictionary

#ex:

emp={"empname":"krishna","jobdesc":"software programmer","salary":34000}

print(emp.values())

print(type(emp.values()))


#items():return all the items present in the dictionary. each item will be inside a tuple as a      key-value pair.

emp={"empname":"krishna","jobdesc":"software programmer","salary":34000}

print(emp.items())

print(type(emp.items()))


#Iterating a dictionary:

emp={"empname":"krishna","jobdesc":"software programmer","salary":34000}

for x in emp:

    print(x)

note:it will display only keys

emp={"empname":"krishna","jobdesc":"software programmer","salary":34000}

print('key', ':', 'value')

for key in emp:

    print(key, ':', emp[key])

# using items() method

print('key', ':', 'value')

for key_value in emp.items():

    print(key_value[0], key_value[1])


#find the length of the dictionary

emp={"empname":"krishna","jobdesc":"software programmer","salary":34000}

print(len(emp))


#adding new keys in dictionary 

emp={"empname":"krishna","jobdesc":"software programmer","salary":34000}

emp["empno"]=101 #single item adding

print(emp)

emp.update({"doj":"12/09/2022","company":"google"})

print(emp)




#python dictionary,#dictionary keys(),#dictionary values(),#dictionary items(),
#python datatypes,#python dict(),#dictionary accessing elements,#all keys(),#all vlaues(), #all items()

Comments

Post a Comment

Popular posts from this blog

remove specific element in python list - remove index based element

  #remove specific element in python list list=list([34,55,34,66,77,45.6,23,34,]) print(list) list.remove(66) print(list) output: [34, 55, 34, 66, 77, 45.6, 23, 34] [34, 55, 34, 77, 45.6, 23, 34] #Remove all occurrence of a specific python repeated element mylist = list([26, 4, 26, 26, 8, 12]) for item in mylist:     mylist.remove(26) print(mylist) output :  [4,8,12] #remove item present at given index mylist = list([2, 4, 6, 8, 10, 12]) mylist.pop(2)  #remove 2 index based element print(mylist) output:  [2, 4, 8, 10, 12] # remove item without passing index number(last value remove) using pop() mylist = list([2, 4, 6, 8, 10, 12]) mylist.pop()   print(mylist) output : [2, 4, 6, 8, 10] [2, 4, 8, 10] #remove the range of elements python list with slice: mylist = list([2, 4, 6, 8, 10, 12]) #remove item from index 2 to 5 del mylist[2:5] print(mylist) output: [2, 4, 12] #remove all items starting from index 2 mylist=list([23,44,55,34,53,78,55]) del myl...

Python tuple packing and unpacking - Python coding

Python tuple packing and unpacking python tuple can also be created without using a tuple() class or enclosing the items inside the parentheses. it is called the variable packing #python tuple packing into tuple tuple=23,44,66,'django' print(tuple) print(type(tuple)) #unpacking tuple into variable a,b,c,d=tuple print("a is =",a) print("b is =",b) print("c is =",c) print("d is =",d) print(type(a)) print(type(b)) print(type(c)) print(type(d)) output :  (23, 44, 66, 'django') <class 'tuple'> a is = 23 b is = 44 c is = 66 d is = django <class 'int'> <class 'int'> <class 'int'> <class 'str'>

Python Complex Datatypes

Python Complex Datatypes    A complex number is a number with a real and an imaginary component represented as a+bj where a and b contain integers or floating-point values. # both values are int type a = 9 + 8j           print(a)      print(type(a)) # one int and one float values b = 10 + 4.5j        print(b)       print(type(b)) # both values are float type c = 11.2 + 1.2j      print(c) print(type(c))