0

I just can not figure it out how to easily do it in python:

myArray = ["this ","is ","a ","test.","this ","is ","another ","test."]

now I want the output to be

  1. print(myArray[0:3]) -> "this is a test"
  2. print(myArray[4:7]) -> "this is another test"

Is there a feature in python that allows this without iterating over the whole array within a for word in myArray ...

What I get is an index in a loop which only tells me up to which word I should "print" it.

I prefer a python "exclusive" variant, which is short and simple, best case a one liner and it should need as less memory as possible (fast even for thousands of attempts)

1
  • 1
    Just join the list elements with a whitespace, i.e.: " ".join(myArray[4:7]) Commented Jul 24, 2018 at 8:48

9 Answers 9

5

You can try join(), I hope this is the solution you are looking for

myArray = ["this ","is ","a ","test.","this ","is ","another ","test."]
print(' '.join(myArray[:4]))
print(' '.join(myArray[4:]))
Sign up to request clarification or add additional context in comments.

Comments

3

It seems like what you actually want is to join together some sublist in your list of words.

>>> myArray = ["this ","is ","a ","test.","this ","is ","another ","test."]
>>> print(''.join(myArray[0:4]))
this is a test.
>>> print(''.join(myArray[4:8]))
this is another test.

2 Comments

oops my bad :facepalm: Time for coffee I guess <g>
thx :) so fast and many answers haha ... guess you was the fastest ^^
2

I guess you have to make it myArray[0:4] and join function...

print("".join(myArray[0:4]))
print("".join(myArray[4:8]))

Comments

2

You need to use the string method join(). This method applies to a separator. For instance:

"".join(myArray[0:4])

Will output:

'this  is  a  test.'

Comments

1

You were close.

For python 2.7:

print " ".join(test[0:3])

For python 3.x:

print(*test[0:3])  

Comments

1

As the other guys have said, you can always use join:

//generate 10 letters
myArray = [chr(letter) for letter in range(97,107)]
//return letters from 0 to 5
"".join(myArray)[0:5]

However, I don't understand why you can't use this format

print[0:5]

In terms of efficiency, the latter option is better, and it is just as pythonic as the former. When you 'join' the array, you are implicitly interating over it, and when you finally display the necessary elements, you are iterating again.

1 Comment

Upvote. Indeed your explanation makes sense for prints. However, the print in OP post is probably to test a piece of code used differently, in which case the join is the correct way to go.
0

just use the join()

first="".join(myArray[0:3])
second="".join(myArray[4:7])

Comments

0

as friends have told you can use join in python:

print("".join(myArray[x:y]))

which puts what you call join on it (here "") between elements as seperator and returns as string. one good way is just remove ending space from your array and do like this:

print(" ".join(myArray[x:y]))

Comments

0

enter image description here

print(''.join(myArray[:4])) -> "this is a test"

print(''.join(myArray[4:])) -> "this is another test"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.