How to put comments inside a Python dictionary? How to put comments inside a Python dictionary?

How to put comments inside a Python dictionary?



You can put comments like you normally would anywhere in a python script. But note that you can only put single line comments using #. Multiline comments act like strings and you cannot put just a string in between definition of a dict. For example, the following declaration is perfectly valid:

testItems = {
   'TestOne': 'Hello',
   # 'TestTwo': None,
}

But the following is not:

testItems = {
   'TestOne': 'Hello',
   """
   Some random
   multiline comment
   """
}
Lakshmi Srinivas
Lakshmi Srinivas

Updated on: 2020-06-17T11:46:03+05:30

565 Views

Kickstart Your Career

Get certified by completing the course

Get Started