I have been looking around to see if anyone has actually done it but couldn't find it so hoping I can get some help here.

newDict = {'Jan':31, 'Feb':29, 'Mar':31, 'Apr':30, 'May':31, 'Jun':30, 'Jul':31, 'Aug':30}

I created this dict and I want to use a while loop to output it this way:

Jan 31
Feb 29
Mar 31
Apr 30
May 31
Jun 30
Jul 31
Aug 30

I am able to do it with a for loop, just curious how it can be done with a while loop.

2 upvote
  flag
I am very curious: Why do you want use a while loop? – Moberg
upvote
  flag
@Moberg Hi, I wanted to try different ways to see how I am able to print dictionary instead of using For loop which is easier to use. – Cassandra
upvote
  flag
You should seriously consider learning Python 3, Python 2 will reach its official End Of Life in 2020. – PM 2Ring

6 Answers 11

Use following code:

key=list(newDict.keys())
i=0
while i<len(key):
    print(key[i]," ",newDict[key[i]])
    i+=1

You can do this with while loop.

newDict = {'Jan':31, 'Feb':29, 'Mar':31, 'Apr':30, 'May':31, 'Jun':30, 'Jul':31, 'Aug':30}
i = 0
while i < len(newDict):
    val = newDict.items()[i]
    print val[0], val[1]
    i += 1
up vote 4 down vote accepted

You can make your dictionary an iterator calling iteritems (Python 2.x), or iter on the items() (Python 3.x)

# Python 2.x
from __future__ import print_function
items = newDict.iteritems()

# Python 3.x
items = iter(newDict.items())

while True:
    try: 
        item = next(items)
        print(*item)
    except StopIteration:
        break

Note: We're importing print_function on Python 2.x because print would be a statement instead of a function, and hence the line print(*item) would actually fail

upvote
  flag
this throws an Error on me 'dict_itemiterator' object has no attribute 'next'. Did you try it? – Ev. Kounis
2 upvote
  flag
There's dict.iteritems(), which is already an iterator. But you should use the next function rather than calling the .next method directly. FWIW, in Python 3, .next doesn't exist, it's now .__next__, making it consistent with the other special methods. – PM 2Ring
upvote
  flag
I haven't tried it on Python 3.x, but it's working on Python 2.x – Matias Cicero
upvote
  flag
@MatiasCicero but Python 2 does not have .items().. – Ev. Kounis
1 upvote
  flag
@Ev.Kounis It does actually – Matias Cicero
upvote
  flag
@PM2Ring That's an excellent point. I'll make the change – Matias Cicero
upvote
  flag
print(*item) wont work in Python 2, so have to use print item[0], item[1]. – Bijoy
upvote
  flag
@Bijoy Nice catch. Updated code – Matias Cicero

This is an absurd requirement, but here is one way to do it:

newDict = {'Jan':31, 'Feb':29, 'Mar':31, 'Apr':30, 'May':31, 'Jun':30, 'Jul':31, 'Aug':30}

while newDict:
  x = next(x for x in newDict)
  print(x, newDict.pop(x))

CAUTION:

After the while has finished executing, newDIct will be empty.

You can use iterator

newDict = {'Jan':31, 'Feb':29, 'Mar':31, 'Apr':30, 'May':31, 'Jun':30, 'Jul':31, 'Aug':30}
a = iter(newDict.iteritems())
default = object()

while a:
    elem = next(a, default)
    # This check is to know whether iterator is exhausted or not.
    if elem is default:
        break
    print "{} {}".format(*elem)

Note: This code tested only in python 2.7

Here's another option, using the .pop method.

newDict = {
    'Jan':31, 'Feb':29, 'Mar':31, 'Apr':30, 
    'May':31, 'Jun':30, 'Jul':31, 'Aug':30
}
t = newDict.items()
while t:
    print '%s %d' % t.pop()

typical output

Jul 31
Jun 30
Apr 30
Aug 30
Feb 29
Mar 31
May 31
Jan 31

This code doesn't modify the contents of newDict, since in Python 2 dict.items() creates a list of the (key, value) pairs of the dictionary. In Python 3 it returns a dynamic View object, which doesn't have a .pop method, so this code won't work there.

Bear in mind that a dict is an unordered collection, so the output order may not be what you expect.

Not the answer you're looking for? Browse other questions tagged or ask your own question.