0

I don't understand how the Python for loop works because I get a different result:

from itertools import groupby

 all = {}
 data = {'date': '10/12/19', 'name': 'jerry', 'id': 'Hello001'}

 for g , k in groupby(data, lambda r: (r[ 'date' ])):
        for i in k:
        #other data
        all.update({
            "date": i[ 'date' ],
            "name": i[ 'name' ],
        })

        print(all)
        for get in all:
            print(get)

Result of first print

{'date': '10/12/19', 'name': 'jerry', 'id': 'Hello001'}

Result of second print in loop

date
name
id

Why does the second print result only print the header?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437

1 Answers1

0

If you want to print key and value you should do this

for key in all:
    print(key, all[key])

Or you can use .items() method

for key, value in all.items():
    print(key, value)
S. Ellis
  • 113
  • 1
  • 9