Iterating and fetching values from dictionary Here is my dictionary: 

c_result1 = {"1": {"time": "89.46%"}, "2": {"date": "10.54%"}}
c_result2 = {"1": {"money": "89.46%"}, "2": {"finance": "10.54%"}}


I want extract, first key value of 1 i.e. time, length of the dictionary may vary.

My code:

c_result1 = {"1": {"time": "89.46%"}, "2": {"date": "10.54%"}}
c_result2 = {"1": {"money": "89.46%"}, "2": {"date": "10.54%"}}
flag = 0
result = []
result1 = ''
result2 = ''
for k,v in c_result1.iteritems():
    for k1,v1 in v.iteritems():
        result1 = k1
        result.append(result1)
        print 'result = ', result1
        flag = 1
        if flag == 1:
            break
    if flag == 1:
        break
for k,v in c_result2.iteritems():
    for k1,v1 in v.iteritems():
        result2 = k1
        result.append(result2)
        print 'result = ', result2
        flag = 1
        if flag == 1:
            break
    if flag == 1:
        break
print 'REsult is : , ', result


What is the standard way to do this, rather then using flag?

After that I want to extract time and money from each result and pack them into list like ['time','money'], but due to flag I have used here, I was not able to do this.
In your code you write

    flag = 1
    if flag == 1:
        break


The if will always be True, so this is just

    flag = 1
    break


You only use flag for another if that also will always be True, so make this

    break
break


Simplifying this to

c_result1 = {"1": {"time": "89.46%"}, "2": {"date": "10.54%"}}
c_result2 = {"1": {"money": "89.46%"}, "2": {"date": "10.54%"}}
result = []
result1 = ''
result2 = ''
for k,v in c_result1.iteritems():
    for k1,v1 in v.iteritems():
        result1 = k1
        result.append(result1)
        print 'result = ', result1
        break
    break
for k,v in c_result2.iteritems():
    for k1,v1 in v.iteritems():
        result2 = k1
        result.append(result2)
        print 'result = ', result2
        break
    break
print 'REsult is : , ', result


Removing the inner prints gives

c_result1 = {"1": {"time": "89.46%"}, "2": {"date": "10.54%"}}
c_result2 = {"1": {"money": "89.46%"}, "2": {"date": "10.54%"}}
result = []
for k,v in c_result1.iteritems():
    for k1,v1 in v.iteritems():
        result.append(k1)
        break
    break
for k,v in c_result2.iteritems():
    for k1,v1 in v.iteritems():
        result.append(k1)
        break
    break
print 'REsult is : , ', result


This can be simplified by using itervalues and not calling iteritems on the inner loop:

c_result1 = {"1": {"time": "89.46%"}, "2": {"date": "10.54%"}}
c_result2 = {"1": {"money": "89.46%"}, "2": {"date": "10.54%"}}
result = []
for v in c_result1.itervalues():
    for k in v:
        result.append(k)
        break
    break
for v in c_result2.itervalues():
    for k in v:
        result.append(k)
        break
    break
print 'REsult is : , ', result


This is actually just the same as calling next twice:

help(next)
#&gt;&gt;&gt; Help on built-in function next in module builtins:
#&gt;&gt;&gt;
#&gt;&gt;&gt; next(...)
#&gt;&gt;&gt;     next(iterator[, default])
#&gt;&gt;&gt;     
#&gt;&gt;&gt;     Return the next item from the iterator. If default is given and the iterator
#&gt;&gt;&gt;     is exhausted, it is returned instead of raising StopIteration.
#&gt;&gt;&gt;


However this is only simpler if you know it's going to succeed:

c_result1 = {"1": {"time": "89.46%"}, "2": {"date": "10.54%"}}
c_result2 = {"1": {"money": "89.46%"}, "2": {"date": "10.54%"}}
result = []

inner = next(c_result1.itervalues())
result.append(next(iter(inner)))

inner = next(c_result1.itervalues())
result.append(next(iter(inner)))

print 'REsult is : , ', result


Either way, one should make a function or use a loop to avoid repeating oneself:

c_result1 = {"1": {"time": "89.46%"}, "2": {"date": "10.54%"}}
c_result2 = {"1": {"money": "89.46%"}, "2": {"date": "10.54%"}}
result = []

for c_result in (c_result1, c_result2):
    for v in c_result.itervalues():
        for k in v:
            result.append(k)
            break
        break

print 'REsult is : , ', result