0

I have been wondering about the use of or for choosing between two items if one has the probability of having the value of None, instead of using if else, (which I think is great btw)

Two examples:

None or [] -> returns []
None or {} -> returns {}

I've tried to use or in a scenario where one variable can be an empty dictionary and other can be an empty list. In this scenario the or returns the list but if the dictionary is non-empty, the dictionary is always chosen over the other variable. Results are the same i in both python 2.7.17 and python 3.6.9.

Examples:

{} or [] -> returns []
{1:2} or [2] -> returns {1:2} 

Questions:

  1. Is there a preference of an empty list over an empty dictionary?
  2. Is there a preference of a non-empty dictionary over an non-empty list?
  3. If there is no preference order, does any other rules/factors determines the outcome of this operation?

Thank you for your time and answers :) Have a good day.

inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241

0 Answers0