I'm making a very basic code that will check if the list in the dictionary length is greater than the integer in the other key of the dictionary.
For example, if I had:
d = {'lst': [1,2,3,4,5] , 'compare': 5}
Would be fine, because the max number of values (or length) of the list can be 5 (less than or equal to is fine).
This on the other hand should throw an assertion error:
d = {'lst': [1,2,3,4,5,6] , 'compare': 5}
because the length of the list in the key 'lst' > 'compare'.
Here's what I tried:
if len(d['lst']) > d['compare']:
assert 'Queue is larger than max capacity'
else:
pass
I'm brand new to using 'assert', so I'm likely using this wrong. If anyone could give me a hand it would be much appreciated!
The assert
statement takes the condition as the first "argument". No if
statement is required.
assert len(d['lst'] <= d['compare']), "Queue is larger than max capacity"
If the condition is false, an AssertionError
(which includes the optional second argument) is raised. Otherwise, nothing happens.
Think of assert foo, bar
as a shortcut for
# assert foo, bar
if foo:
raise AssertionError(bar)