• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Get Python Dictionary Keys as List

Get Python Dictionary Keys as List

August 19, 2022 Leave a Comment

To get the values of a dictionary in a list in Python, you can use the dictionary values() function and convert it to a list with list().

d = {"a":3, "b": 5, "c":1, "d":2}

list_of_dict_keys = list(d.keys())

print(list_of_dict_keys)

#Output:
[3, 5, 1, 2]

When working with dictionaries, the ability to get information about the keys and values easily can be valuable.

One such case is if you want to get the keys of a dictionary and convert the keys to a list.

To get the keys of a dictionary as a list in Python, you can use the dictionary keys() function and convert it to a list with list().

Below shows you how to get dictionary keys into a list in Python.

d = {"a":3, "b": 5, "c":1, "d":2}

list_of_dict_keys = list(d.keys())

print(list_of_dict_keys)

#Output:
[3, 5, 1, 2]

Get Values of Dictionary as List in Python

If you want to get the values of a dictionary in Python as a list, then you use a similar method as described above.

Instead of using the keys() dictionary function, you can use the values() dictionary function.

values() returns the values of a dictionary.

To get the values of a dictionary as a list in Python, you can use the dictionary values() function and convert it to a list with list().

Below shows you how to get dictionary values into a list in Python.

d = {"a":3, "b": 5, "c":1, "d":2}

list_of_dict_values = list(d.values())

print(list_of_dict_values)

#Output:
[3, 5, 1, 2]

Hopefully this article has been useful for you to learn how to get the keys of a dictionary into a list using Python.

Other Articles You'll Also Like:

  • 1.  Using Python to Convert Decimal to String
  • 2.  Check if Variable is None in Python
  • 3.  PROC REG Equivalent in Python
  • 4.  Python Negative Infinity – How to Use Negative Infinity in Python
  • 5.  Ceiling Division in Python
  • 6.  Get Last Character in String in Python
  • 7.  Get Difference Between datetime Variables in Python
  • 8.  Calculate Distance Between Two Points in Python
  • 9.  How to Declare Variable Without Value in Python
  • 10.  Using Python to Check if Number is Divisible by Another Number

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy