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

Software Jobs - Web Developer jobs in Andhra Pradesh, Rajahmundry,

Software Jobs - Web Developer jobs in Andhra Pradesh, Rajahmundry,  Openings: Web Developer  Qualification: BE,Btech,MCA,MSC Experience: 1-5 years Requirements: Should have good experience as a PHP Application developer Hands on Experience in developing the application using MVC architecture. Good to have knowledge in MySQL, Codeignitor, Zend, CakePHP, Joomla, Magneto and Laravel is a plus. Have experience with REST API, JSON, and SOAP. Expertise in HTML5, CSS, Bootstrap, jQuery, Ajax. Experience in taking ownership of product delivery, code quality and architecture. Should be proactive, goal-oriented, reliable and a have a self-structured way of working. Responsibilities: Creating website layout/user interfaces by using standard HTML/CSS practices. Designing well organized databases. Integrating data from various back-end services and databases Be involved in al...

Adding and changing elements in a tuple using insert() and append methods

  Adding and changing elements in a tuple using insert and append: A list is a mutable type, which means we can add or modify values in it, but tuples are immutable, so they cannot be changed. #how to add new element in tuple object using index based tup=(6,75,66,34,23) print(tup) #print all tuple elements newlist=list(tup) #tuple to list convert newlist.insert(2,40) #add new element in list tup=tuple(newlist) #convert list to tuple print(tup) #print all elements in tuple output: (6, 75, 66, 34, 23) (6, 75, 40, 66, 34, 23) #how to add new element in tuple object using apped() method tup=(6,75,66,34,23) newlist=list(tup) newlist.append(22) tup=tuple(newlist) print(tup) output: (6, 75, 66, 34, 23, 22) #Modify nested items of a tuple: then we can change its values in the case of a nested tuple. Ex: tuple1 = (10, 20, [25, 75, 85]) print(tuple1) tuple1[2][0] = 250 print(tuple1) output: (10,20,[250,75,85])      #python tuples, #python training in guntur, #python jobs in gu...

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))