I need to create a new value in the array when theres a space (python)


Ive got an issue where i dont know how to split an array up when it finds a specific charater and i havn’t been able to find anything online about it.

For example:
ive got an array with a single string in it

Array = ['SomeText SomeMoreText EvenMoreText']

At every space i want everything to the left of it to become its own value. ive probablly explained this horribly but this is the result i want…

Array = ['SomeText', 'SomeMoreText', 'EvenMoreText'] 

It splits it up every time theres a space

Asked By: Alex Holland



Try this:

arr = ["Here is some text"]
arr = arr[0].split()
//['Here', 'is', 'some', 'text']

The split() method called for arr[0] splits the 0th element of arr by whitespaces (by default). Then the return of the method is put into the arr array.

Answered By: DGMexe

You said:

For example: ive got an array with a single string in it

Since there is a single string in the list.

Array = ['SomeText SomeMoreText EvenMoreText']

you can do:

Array = Array[-1].split()


Array = Array[0].split()
Answered By: God Is One
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.