Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate the keys and values from the dictionary

How can I access the dictionary's key and value and iterate over for loop?

dictionary = {1: "one", 2: "two", 3: "three"}

My output will be like:

1  one
2  two
3  three
like image 524
gm03 Avatar asked Nov 01 '25 01:11

gm03


1 Answers

You can use this code snippet.

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

# To iterate over the keys
for key in dictionary.keys():  # or `for key in dictionary`
    print(key)

# To iterate over the values
for value in dictionary.values():
    print(value)

# To iterate both the keys and values
for key, value in dictionary.items():
    print(key, '\t', value)
like image 169
Gowdham V Avatar answered Nov 03 '25 17:11

Gowdham V



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!