How to return dictionary keys as a list in Python

Ads
 

How to return dictionary keys as a list in Python

Hi,

I want to access the keys of dictionary as list in Python program.

Here is dictionary data:

word_dict = {1:"one", 2:"Two", 3:"Three",
    4:"Four", 5:"Five", 6:"Six", 7:"Seven",
    8:"Eight", 9:"Nine", 10:"Ten"}
print(word_dict)

How to get all keys as list?

Thanks

View Answers

December 25, 2018 at 9:04 AM

Hi,

You can use following code to get all keys as list from dictionary:

my_list = list(word_dict.keys())
print(my_list)

Here is output:

>>> my_list = list(word_dict.keys())
>>> print(my_list)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

For printing all keys you can use following code:

for key in word_dict.keys():
  print(key)

Output:

>>> for key in word_dict.keys():
...   print(key)
... 
1
2
3
4
5
6
7
8
9
10
>>>

Python 3 support unpacking and it can be used to get all the keys as list. Here is example code:

my_list2 = *word_dict,
print(my_list2)

Output of the program:

>>> my_list2 = *word_dict,
>>> print(my_list2)
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
>>>

Hope this helps you.

Thanks


December 25, 2018 at 9:05 AM

Hi,

Check Python Dictionary tutorial for more details about the methods of dictionary in Python.

Thanks

Ads









Related Tutorials/Questions & Answers:
how to read from dictionary c
how to read from dictionary c  how to read elements from dictionary in c programming
how to write a english dictionary project in java
how to write a english dictionary project in java  please give me idea how to write program/source code for dictionary project
Advertisements
PHP list array keys
array_keys() function returns the keys of the the given array. It returns the keys in the form of array. Example of PHP list Array Keys <?php...;   $key=array_keys($ar1);     print_r($key); ?>
How to import elglish dictionary in java - Java Beginners
How to import elglish dictionary in java  Hi.. This is Sakthi... How to import english dictionary in java platform.. My task it to check the inserted characters are meaningful or not? Can any one help me out
how to import english dictionary in java - Java Beginners
how to import english dictionary in java  Hi.. This is Sakthi... How to import english dictionary in java platform.. My task it to check whether the entered characters are meaningful or not? Can any one help me out
How to run external command in Python?
How to run external command in Python?  Hi, I am writing an application in Python and there is requirement of calling the external command from... external command. How to run external command in Python? Thanks   Hi
how to create customise shortcut keys in jsp?
how to create customise shortcut keys in jsp?  I want to use shortcut keys in my application. keys like' EX: 1)S-Slag 2)ex-Exposure etc
Version of com.clearnlp>clearnlp-dictionary dependency
List of Version of com.clearnlp>clearnlp-dictionary dependency
Version of com.hynnet>jradius-dictionary dependency
List of Version of com.hynnet>jradius-dictionary dependency
Version of com.hynnet>jradius-dictionary-min dependency
List of Version of com.hynnet>jradius-dictionary-min dependency
in string how will write the return statement..........?
in string how will write the return statement..........?  import java.util.Scanner; class BinaryConversion12{ static int j=1; static int l=1; public static void main(String args[]){ int i=Integer.parseInt(args[0
How to return a string from a JTextField
How to return a string from a JTextField  Hi, Am new to java, and am trying to get a string from a JTextField so that I can compare it with another.... The problem is i have no clue how to get the very string i need because i think
Version of com.noodlesandwich>rekord-keys dependency
List of Version of com.noodlesandwich>rekord-keys dependency
Version of com.noodlesandwich>rekord-keys dependency
List of Version of com.noodlesandwich>rekord-keys dependency
Version of com.datomic>s3-access-keys dependency
List of Version of com.datomic>s3-access-keys dependency
Version of cocoon>cocoon-python dependency
List of Version of cocoon>cocoon-python dependency
Version of drools>drools-python dependency
List of Version of drools>drools-python dependency
Dictionary class
Dictionary class  hello,, What is the Dictionary class?   hii,, The Dictionary class provides the capability to store key-value pairs
How to get Keys and Values from HashMap in Java?
How to get Keys and Values from HashMap in Java? Example program of iterating... to iterate the keys and get the values and print on the console. How to get keys... to iterate through keys and get the values. So, let's see how to iterate
Version of plexus>plexus-python-factory dependency
List of Version of plexus>plexus-python-factory dependency
Java dictionary class
Java dictionary class  What is the Dictionary class
create dictionary in java
create dictionary in java  Hi, dear : How can create simple dictionary in java .. by using Applet or any other way like GUI (basic), please write Comment on the line that difficult to learning it .think u so much
how to return to main menu after adding all the info. - Java Beginners
how to return to main menu after adding all the info.  import.... Remove a student"); System.out.println("3. Display student list...() { return id; } public String getName() { return name; } } public class
Write the Keys and Values of the Properties files in Java
will learn how to add the keys and it's values of the properties files in Java. You.... But this time you will learn, how to insert the keys and values in the properties... Write the Keys and Values of the Properties files in Java
setting keys
setting keys  Hi Sir\Mam,At present we are doing Payroll Project .I would like to set some special keys for my forms and i want the coding to do the same .Pls Provide me source code for setting keys as soon as possible
Generate Keys
Generate Keys  hello i need code to generate key , after that send Keys to email address user. for example if you member in your website,if member not having Secret Keys not can download files if member having secret key i
Keys in DBMS
Keys in DBMS  Whats the main difference between composite and super keys   Difference between super key and composite key: A super key... in a table while composite key is the combination of two or more than two keys
How to return multiple values from a method - Java Beginners
How to return multiple values from a method  I came the following method "How do I return multiple values from a method?" posted elsewhere... result. Can someone tell me how my Driver.java program [see below] should
How to return multiple values from a method - Java Beginners
How to return multiple values from a method  I came the following method "How do I return multiple values from a method?" posted elsewhere... result. Can someone tell me how my Driver.java program [see below] should
How to get javascript function return value in xsl - XML
How to get javascript function return value in xsl  Hi friends , i have a doubt on xsl, i want to know how to get value from javascript function to xsl variable with small piece of code,i need immediately , if anybody plz send ur
Get All Keys and Values of the Properties files in Java
Get All Keys and Values of the Properties files in Java       In this section, you will learn how to get all keys and it's values of the properties files in Java. Java provides

Ads