2

For some implementation purpose, I have generated a list of dictionaries. I am providing a shorter version of the list here.

dicto = [ {'type': 'convolutional', 'batch_normalize': '1', 'filters': '128', 'size': '3', 'stride': '2', 'pad': '1', 'activation': 'leaky'}, {'type': 'convolutional', 'batch_normalize': '1', 'filters': '64', 'size': '1', 'stride': '1', 'pad': '1', 'activation': 'leaky'}]

Now ,if I print dicto..

print(dicto)

It prints the exact value, but if try to get the value of dicto , then it return some other values. If I run..

dicto

This value it returns..

  [{'activation': 'leaky',
  'batch_normalize': '1',
  'filters': '128',
  'pad': '1',
  'size': '3',
  'stride': '2',
  'type': 'convolutional'},
 {'activation': 'leaky',
  'batch_normalize': '1',
  'filters': '64',
  'pad': '1',
  'size': '1',
  'stride': '1',
  'type': 'convolutional'}]

I often use returning value and print alternatively(though it not might be generalized practice), and I also know that they often behave differently, specially for pandas dataframe. But it have never occurred to me that the returned value and printed valued is that much different. Besides it has even changed the serial of the list elements.

Can you please figure me out why this happens?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Sawradip Saha
  • 1,151
  • 11
  • 14
  • It is the same dictionary with just a different order of key-value pair. Take a closer look at it. – Dhaval Taunk Jul 17 '20 at 05:33
  • `return` and `print` have **nothing to do with each other**. Likely, you are working *in a REPL*, like the default REPL, or an IPython REPL, in which case, whenever you evaluate an expression, it *prints* it automatically (a REPL is a *R*ead *E*valuate *P*rint *L*oop). It is quite imporant to understand the difference between printing and returning, again, they are **two completely different things**. I suspect you are using IPython, which pretty-prints various data structures, and that difference is confusing you. – juanpa.arrivillaga Jul 17 '20 at 05:35
  • dictionaries are guaranteed to be ordered by insertion as of python 3.7. – Trenton McKinney Jul 17 '20 at 06:30
  • Does this answer your question? [Are dictionaries ordered in Python 3.6+?](https://stackoverflow.com/questions/39980323/are-dictionaries-ordered-in-python-3-6) – Trenton McKinney Jul 17 '20 at 06:33

2 Answers2

0

Actually both the result you got in 'print(dicto)' and 'dicto' are same as your input but it is order by the alphabetical order of 'keys'.

If you want to preserve order of the keys as you have declared you can achieve it by the usage of 'OrdererdDict'. Here is the code.

Method1: Manual convertion

Code:

#import OrdererdDict module from collections
from collections import OrderedDict

dicto = [ OrderedDict({'type': 'convolutional', 'batch_normalize': '1', 'filters': '128', 'size': '3', 'stride': '2', 'pad': '1', 'activation': 'leaky'}), OrderedDict({'type': 'convolutional', 'batch_normalize': '1', 'filters': '64', 'size': '1', 'stride': '1', 'pad': '1', 'activation': 'leaky'})]

print(dicto)

Output:

[OrderedDict([('type', 'convolutional'), ('batch_normalize', '1'), ('filters', '128'), ('size', '3'), ('stride', '2'), ('pad', '1'), ('activation', 'leaky')]), OrderedDict([('type', 'convolutional'), ('batch_normalize', '1'), ('filters', '64'), ('size', '1'), ('stride', '1'), ('pad', '1'), ('activation', 'leaky')])]

You can get the same result without print() function also.

dicto

Output

[OrderedDict([('type', 'convolutional'),
          ('batch_normalize', '1'),
          ('filters', '128'),
          ('size', '3'),
          ('stride', '2'),
          ('pad', '1'),
          ('activation', 'leaky')]),

OrderedDict([('type', 'convolutional'), ('batch_normalize', '1'), ('filters', '64'), ('size', '1'), ('stride', '1'), ('pad', '1'), ('activation', 'leaky')])]

N.B: Don't worry about the OrderedDict comes in the display. It won;t affect the values. Here is the proof that you can access anything.

Accessing first dictionary from the list dicto:

dicto[0]

Output:

OrderedDict([('type', 'convolutional'),
         ('batch_normalize', '1'),
         ('filters', '128'),
         ('size', '3'),
         ('stride', '2'),
         ('pad', '1'),
         ('activation', 'leaky')])

Accessing value of key 'filters' of first dictionary from the list dicto expected value is '128':

dicto[0]['filters']

Output:

'128'

Method2: Automatic convertion

If you didn't understand and the first method looks so messy you can follow the second method. Here we iterate through you each dictionary elements in the list and makes it to Orderdict by using list comprehension method. Here is the code.

Code:

#import OrderedDict module from collections
from collections import OrderedDict

#your list dicto declared here
dicto = [ {'type': 'convolutional', 'batch_normalize': '1', 'filters': '128', 'size': '3', 'stride': '2', 'pad': '1', 'activation': 'leaky'}, {'type': 'convolutional', 'batch_normalize': '1', 'filters': '64', 'size': '1', 'stride': '1', 'pad': '1', 'activation': 'leaky'}]

#converting each dictionary element to oredered dictionaries by list comprehension
dicto = [OrderedDict(x) for x in dicto]

#display resulting list dicto
print(dicto)

Output:

[OrderedDict([('type', 'convolutional'), ('batch_normalize', '1'), ('filters', '128'), ('size', '3'), ('stride', '2'), ('pad', '1'), ('activation', 'leaky')]), OrderedDict([('type', 'convolutional'), ('batch_normalize', '1'), ('filters', '64'), ('size', '1'), ('stride', '1'), ('pad', '1'), ('activation', 'leaky')])]

I hope this would be helpful... :)

Littin Rajan
  • 852
  • 1
  • 10
  • 21
0

To check if 2 dictionaries are the same, use assert:

dicto = [{'type': 'convolutional',
          'batch_normalize': '1',
          'filters': '128',
          'size': '3',
          'stride': '2',
          'pad': '1',
          'activation': 'leaky'
          },
         {'type': 'convolutional',
          'batch_normalize': '1',
          'filters': '64',
          'size': '1',
          'stride': '1',
          'pad': '1',
          'activation': 'leaky'
          }
         ]

returned = [{'activation': 'leaky',
             'batch_normalize': '1',
             'filters': '128',
             'pad': '1',
             'size': '3',
             'stride': '2',
             'type': 'convolutional'},
            {'activation': 'leaky',
             'batch_normalize': '1',
             'filters': '64',
             'pad': '1',
             'size': '1',
             'stride': '1',
             'type': 'convolutional'}
            ]

assert dicto == returned

Then visually verify the items in them side by side if you like:

sorted_dicto = [sorted(d.items()) for d in dicto]
sorted_dicto = [j for i in sorted_dicto for j in i]
sorted_returned = [sorted(d.items()) for d in returned]
sorted_returned = [j for i in sorted_returned for j in i]

for d1, d2 in zip(sorted_dicto, sorted_returned):
    print(f"{d1[0]}  {d2[0]:>30}")
    print(f"{d1[1]}  {d2[1]:>30}")

Returning:

activation                      activation
leaky                           leaky
batch_normalize                 batch_normalize
1                               1
filters                         filters
128                             128
pad                             pad
1                               1
size                            size
3                               3
stride                          stride
2                               2
type                            type
convolutional                   convolutional
activation                      activation
leaky                           leaky
batch_normalize                 batch_normalize
1                               1
filters                         filters
64                              64
pad                             pad
1                               1
size                            size
1                               1
stride                          stride
1                               1
type                            type
convolutional                   convolutional

Process finished with exit code 0
Gustav Rasmussen
  • 3,720
  • 4
  • 23
  • 53