Python: Loop Thru Dictionary

By Xah Lee. Date:

Loop thru a dictionary's keys.

# -*- coding: utf-8 -*-
# python
# loop thru a dictionary's keys

dd = {'john':3, 'mary':4, 'jane':5, 'vicky':7}

for kk in dd:
    print kk

# jane
# john
# mary
# vicky

Loop thru a dictionary, get both key and value.

# -*- coding: utf-8 -*-
# python
# Loop thru a dictionary, get both key and value.

dd = {'john':3, 'mary':4, 'jane':5, 'vicky':7}

for kk, vv in dd.iteritems():
    print kk, ' is ', vv

# jane  is  5
# john  is  3
# mary  is  4
# vicky  is  7

Ask me question on patreon