How to Obtain a List of all Keys of a Dictionary in Python


In this article, we show how to obtain a list of all keys of a dictionary in Python.

Basically, we can list all the keys.

Remember that a dictionary is composed of key/value pairs.

If for example, we have a dictionary composed of stocks and their stock prices, the keys would be the stocks and the values would be the stock prices.

We can list all of the keys of a dictionary by using the list() function and specifying the name of the dictionary as a parameter.

This is shown in the code below.

So, you can see we have created a dictionary called stocks.

These include the stocks of amazon, apple, google and verizon.

To list all of the keys of this dictionary, we use the list() function and specify the dictionary as its parameter.

We get a list as the output.

If you want, you can also store this in a variable.

And this is a very easy way of listing all of the keys of a dictionary in Python.

Related Resources

How to Show All Tables of a MySQL Database in Python

How to Count the Number of Rows in a MySQL Table in Python

HTML Comment Box is loading comments...