Jordan Taylor
Jordan Taylor

Reputation: 15

How to append an array to an array in python

I'm trying to append an array new to an existing empty array ra like this:

ra.append(new)

However, when I run this code and write ra, it looks like this:

ra = [array([ 103.290577,  103.312447,  103.371779,  103.376812,  103.38486 ,...]]

All of my values are appending correctly and I am getting a 2d array, but there is this string "array" at the beginning of every array inside ra. What is causing this and how can I correct my problem?

I definitely need to use append as I need an array of arrays. My problem is when I write ra, I get this string "array(" in front of every new array. Why is this happening is my question.

Upvotes: 0

Views: 841

Answers (2)

The Brofessor
The Brofessor

Reputation: 1048

My problem is when I write ra, I get this string "array(" in front of every new array. Why is this happening is my question.

You do not need to worry about this, it just means that your list was initialized as an array from some import functionality. Not sure what that is without a larger code sample, but it looks like numpy or array.

If you have used import array there is a simple function that turns it back into a list and removed that header: new_list = new.tolist()

Upvotes: 0

Shreyas Chavan
Shreyas Chavan

Reputation: 1099

Are you looking for ra.extend(new) ? Documentation here

Edit: OP's comment: From what I have read, I thought that extend would add my values, but I would have one long array. I need a 2d array of these values.

Yes extend() will create one long array from elements of ra and new.AFAIK no inbuilt function is available for create a 2d array from two 1d arrays. Although if you have all numbers in ra and new, you can use numpy and use column_stack or vstack to stack ra and new together to create a 2d array.

Upvotes: 1

Related Questions