1

I have a list, d2_sum, that contains numpy arrays and python lists, and I'm trying to convert all to python lists so I can perform some slicing operations. I have,

d2_sum_list = [i.tolist() if type(i) == 'numpy.ndarray' else i for i in d2_sum]

This executes with no errors but does not convert any of the numpy arrays to python lists. What am I missing here?

Jonny1998
  • 107
  • 1
  • 1
  • 13

1 Answers1

1

remove the quotes, because type(something) == "numpy.ndarray" will always be False:

d2_sum_list = [i.tolist() if type(i) is numpy.ndarray else i for i in d2_sum]
asdf101
  • 569
  • 5
  • 18
  • More idiomatically, you'd use `type(i) is numpy.ndarray`, or `isinstance(i, numpy.ndarray)`, although those are not exactly the same (although effectively the same here) – juanpa.arrivillaga Apr 14 '21 at 20:37
  • very good point actually, I tend to forget `is` exists unless I check for None – asdf101 Apr 14 '21 at 20:39