0

I wrote a script that loops through the dictionary and prints keys and values separately. The code is as follows:

dict = {"key1":"value1","key2":"value2"}

list1 = list(dict.keys())
list2 = list(dict.values())

i = 0

while i < len(list1)
    print(list1[0])

When I execute the script, key1 prints multiple times, while I expect key1, key2 to be printed.

Andrew Li
  • 55,805
  • 14
  • 125
  • 143
sudhir kumar
  • 115
  • 1
  • 11
  • This has an assortment of issues, you `while` without incrementing `i` (infinite loop), you index `list1` using the same value (same key printed), you don't have `:` after the while statement (`SyntaxError`). I linked the appropriate way to loop through a dictionary, you'll find it is way better to use a `for i in dictionary` construct. – Dimitris Fasarakis Hilliard Sep 27 '16 at 22:04
  • I have been through the linked question. I used colon in my code. I don't understand why while loop is not iterating? – sudhir kumar Sep 27 '16 at 22:10
  • Morevover I am trying to pass these values to python function variables – sudhir kumar Sep 27 '16 at 22:14
  • Issue resolved now – sudhir kumar Sep 27 '16 at 22:30

0 Answers0