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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.