Heterogeneous lists

What if the lists were made of heterogeneous elements, such as integers, floats, and strings? This gets trickier. A quick example can describe the situation to you:

In: import numpy as np    complex_list = [1,2,3] + [1.,2.,3.] + ['a','b','c']       # at first the input list is just ints    Array_2 = np.array(complex_list[:3])     print ('complex_list[:3]', Array_2.dtype)    # then it is ints and floats    Array_2 = np.array(complex_list[:6])      print ('complex_list[:6]', Array_2.dtype)    # finally we add strings print    Array_2 = np.array(complex_list)      ('complex_list[:] ',Array_2.dtype) Out: complex_list[:3] int64      complex_list[:6] float64      complex_list[:] <U32

As explicated by our output, it seems that float types prevail over int types, and ...

Get Python Data Science Essentials - Third Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.