Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to iterate through keys and values in a dictionary in python [duplicate]

Tags:

python

I have created a dictionary which has data like this:

0 = {'Input_file': 'Sample_ Data.xlsx', 'Column_Name': 'Status', 'Column_Filter': 'Active'}

1 = {'Input_file': 'Sample_ Data.xlsx', 'Column_Name': 'Status', 'Column_Filter': 'Inactive'}

how can I iterate through only the values in the same order as 0 and 1 without the keys getting printed. I want to read :- Sample_Data.xlsx , Status, Active in the first iteration and Sample_Data.xlsx , Status, Inactive in the second.

Below is the code I am using to print.

my_dict = reference.to_dict('index')
for column,values in my_dict.items():
    print("{} = {}".format(column, values))
like image 288
anky Avatar asked Sep 18 '25 13:09

anky


1 Answers

You can use list(dict.keys()) to get the keys as a list. As N_tro_P pointed out, you can use sorted(dict.keys()) to get keys in sorted order and iterate with that.

like image 56
dhilmathy Avatar answered Sep 21 '25 02:09

dhilmathy