Learn Python Training from the Best Tutors
Search in
In Python, the keys()
method is used to get a list of all the keys in a dictionary. Here's an example:
my_dict = {'a': 1, 'b': 2, 'c': 3}
keys_list = my_dict.keys()
print(keys_list) # output: ['a', 'b', 'c']
In this example, my_dict.keys()
returns a list of all the keys in the my_dict
dictionary, which are then assigned to the keys_list
variable. The print()
function is used to display the contents of the keys_list
variable.
Note that the keys()
method returns a view object in Python 3.x, which is a dynamic view on the dictionary's keys that reflects any changes to the dictionary. In Python 2.x, keys()
returns a list of the dictionary's keys.
You can use the keys()
method to iterate over the keys of a dictionary using a for
loop, like this:
my_dict = {'a': 1, 'b': 2, 'c': 3}
for key in my_dict.keys():
print(key)
This will print out each key in the my_dict
dictionary on a separate line.
read less
In Python, .keys()
is a method that can be used on a dictionary object to return a view object containing the keys of the dictionary. Here's an example of how to use it:
# Create a dictionary
my_dict = {'name': 'John', 'age': 25, 'gender': 'Male'}
# Get a view object containing the keys of the dictionary using .keys()
keys = my_dict.keys()
# Print the view object
print(keys)
Output:
dict_keys(['name', 'age', 'gender'])
read lessView 1 more Answers
Related Questions
Now ask question in any of the 1000+ Categories, and get Answers from Tutors and Trainers on UrbanPro.com
Ask a QuestionRecommended Articles
Top 5 reasons why you should learn Python
Python is one of the most popular programming languages in the world. It is general-purpose, object oriented, high-level programming language used in a number of programming fields. Python is a great programming language to learn as it will introduce you to the world of programming. If you are from the technical background...
Why Should you Become an IT Consultant
Information technology consultancy or Information technology consulting is a specialized field in which one can set their focus on providing advisory services to business firms on finding ways to use innovations in information technology to further their business and meet the objectives of the business. Not only does...
Learn Hadoop and Big Data
Hadoop is a framework which has been developed for organizing and analysing big chunks of data for a business. Suppose you have a file larger than your system’s storage capacity and you can’t store it. Hadoop helps in storing bigger files than what could be stored on one particular server. You can therefore store very,...
Make a Career as a BPO Professional
Business Process outsourcing (BPO) services can be considered as a kind of outsourcing which involves subletting of specific functions associated with any business to a third party service provider. BPO is usually administered as a cost-saving procedure for functions which an organization needs but does not rely upon to...
Looking for Python Training classes?
Learn from the Best Tutors on UrbanPro
Are you a Tutor or Training Institute?
Join UrbanPro Today to find students near youThe best tutors for Python Training Classes are on UrbanPro
The best Tutors for Python Training Classes are on UrbanPro