1

I have the following python dictionary with bytearray as values

test_bytearray_dict=
{'test_array0':  bytearray(b'0000000000000000'),
 'test_array1':  bytearray(b'0000000001000000'),
 'test_array2':  bytearray(b'0000000002000000'),
 'test_array3':  bytearray(b'0000000000000000'),
 'test_array4':  bytearray(b'0000000001000000'),
 'test_array5':  bytearray(b'0000000002000000'),
 'test_array6':  bytearray(b'0000000000000000'),
 'test_array7':  bytearray(b'0000000001000000'),
 'test_array8':  bytearray(b'0000000002000000'),
 'test_array9':  bytearray(b'0000000000000000'),
 'test_array10': bytearray(b'0000000001000000'),
 'test_array11': bytearray(b'0000000002000000'),
 'test_array12': bytearray(b'0000000000000000'),
 'test_array13': bytearray(b'0000000001000000'),
 'test_array14': bytearray(b'0000000002000000'),
 'test_array15': bytearray(b'0000000000000000'),
 'test_array16': bytearray(b'0000000001000000'),
 'test_array17': bytearray(b'0000000002000000')}

I want to slice every 3rd byte value in the dictionary and compare them if they are equal after slicing certain values. I tried the classic if not and dict.get(key) methods but I am looking for another shorter way

if not test_bytearray_dict.get("test_array0")[3:] == test_bytearray_dict.get("test_array3")[3:]== ...:
   print('error:")

1 Answers1

0

IIUC, you can try all():

vals = iter(test_bytearray_dict.values())
first = next(vals)[3:]

print(all(first == v[3:] for v in vals))
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91