5

How to compare two lists or dictionaries in easy way,

eg.

assert orig_list == new_list

If I want to check two lists in python nose tests,

Is there any built-in function can let me use?

Does compare two lists is a bad practice when doing testing ?(because I've never see it)

If there is no built-in, plugin in nose, is there any handy package can do it for me.

newBike
  • 14,385
  • 29
  • 109
  • 192

3 Answers3

11

You can use assertListEqual(a, b) and assertDictEqual(a, b) from the unittest library.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
psytau
  • 142
  • 4
1

set is used to do that between two lists/dicts!

set(orig_list) & set(new_list)
Sharif Mamun
  • 3,508
  • 5
  • 32
  • 51
  • Yes, doing an `assert_equals(set(orig_list), set(new_list))` prints nice output. You just have to be careful that sometimes you might not want set comparison (when duplicates occur). – metakermit Sep 08 '14 at 16:09
0

This is one way to do it. Manually checking every element for equality.

(len(a) == len(b)) and  (all(ai == bi for ai,bi in zip(a,b)))
M4rtini
  • 13,186
  • 4
  • 35
  • 42