2
\$\begingroup\$

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.

\$\endgroup\$
4
  • \$\begingroup\$ Note that dictionaries are unordered, so taking the first item like this can actually give you any one of the items. Are you trying to get the item with the smallest key? \$\endgroup\$ Jan 9, 2015 at 11:35
  • \$\begingroup\$ @JanneKarila: no I was trying to get first key in dictionary \$\endgroup\$ Jan 9, 2015 at 12:19
  • 2
    \$\begingroup\$ But what does that mean? The key that was first inserted into the dictionary? Then you need collections.OrderedDict that maintains the order. \$\endgroup\$ Jan 9, 2015 at 12:23
  • \$\begingroup\$ I meant first key in both dictionary, time and money in mentioned dictionaries \$\endgroup\$ Jan 9, 2015 at 13:15

1 Answer 1

4
\$\begingroup\$

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)
#>>> Help on built-in function next in module builtins:
#>>>
#>>> next(...)
#>>>     next(iterator[, default])
#>>>     
#>>>     Return the next item from the iterator. If default is given and the iterator
#>>>     is exhausted, it is returned instead of raising StopIteration.
#>>>

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
\$\endgroup\$
2
  • \$\begingroup\$ thanks again, can you please add explaination in get_first ? I did not understand callback=lambda x: x and dict2d. And yeah I need 15 points to upvote your answer : ) \$\endgroup\$ Jan 9, 2015 at 12:16
  • \$\begingroup\$ dict2d is just the name, which will be c_result1 in the first case and c_result2 in the second. I added a little bit about lambda x: x. \$\endgroup\$
    – Veedrac
    Jan 9, 2015 at 12:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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