From the course: NumPy Essential Training: 1 Foundations of NumPy

Unlock the full course today

Join today to access over 24,700 courses taught by industry experts.

Creating arrays from lists and other Python structures

Creating arrays from lists and other Python structures

From the course: NumPy Essential Training: 1 Foundations of NumPy

Creating arrays from lists and other Python structures

- Sometimes we want to construct NumPy arrays directly from Python objects, like list, tuples or list of lists. Let's first learn how to create an ndarray from a Python list. First, we need to import NumPy as NP. I'm going to create a list, first list, that contains integers from one to 10. Go ahead and display that list. Now we'll call NP array and cast that list as an array and assign it in the same line. First array, equals NP array, first list. You can see we got one-dimensional array back as the container for this list object. The elements of NumPy array are usually numbers but they can also be strings, (indistinct) or other objects. But what happens when we create an ndarray from a list that has elements that are not of the same type? So we'll create a list and call it second list. Run the code and then create an array from that list second array. Now let's display our array. As we can see, the array function has automatically promoted all of the numbers to the type of the most…

Contents