Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




keys( ) FUNCTION


The keys( ) Function is used to Iterate a 'Dictionary' and display the 'Keys' .


Example :


x = {
    5 : "Is a Number", 
    "John": "Is a Name", 
    "Python": "Is a Language"
}
for i in x.keys():
    print(i)


Output :



  5
  John
  Python

So , in the above code we have created a 'Dictionary' using braces '{ }' and 'Key' and 'Value' pairs.


x = {
    5 : "Is a Number", 
    "John": "Is a Name", 
    "Python": "Is a Language"
}

And initialised to the variable 'x' .


java_Collections

In the next line we have used the 'for loop' to Iterate through the 'Dictionary' just to get the 'Keys' .


for i in x.keys():
    print(i)

Now , since there is a 'Key' and 'Value' pair in a 'Dictionary' , we have used the 'keys( )' Function, that will only print the 'Keys' and not the 'Values' .


And if you see the Output , just the 'Keys' are printed.


Output :



  5
  John
  Python