2

Is there any other optimized and simple way to store inputs into a list of string.

Input

bcdef
abcdefg
bcde
bcdef

output

['bcdef', 'abcdefg', 'bcde', 'bcdef']

below is the code I tried to code

l = []
for i in range(4):
    l.append(input())
print(l)
3
  • 1
    The code you posted works fine for me. What's the problem? Commented Sep 27, 2019 at 17:20
  • yeah I know that's fine I am figuring is there any other best way to do tath Commented Sep 27, 2019 at 17:21
  • If you want to iterate over stdin, maybe you want fileinput? Commented Sep 27, 2019 at 17:23

3 Answers 3

4

You could also write this as a list comprehension, like this:

l = [input() for i in range(4)]
print(l)
Sign up to request clarification or add additional context in comments.

Comments

1

You can try the following:

import sys
l = sys.stdin.read().split()

You will need to run Ctrl+D once you have entered you input.

2 Comments

actually I am solving the problem in hackerank i don't is this works
@savershkumar, just try it and let me know
0

This works:

l = [input(), input(), input(), input()]

2 Comments

this will work as long as he has to enter 4 strings.
The provided answer was flagged for review as a Low Quality Post. This provided answer may be correct, but it could benefit from an explanation. Code only answers are not considered "good" answers. Here are some guidelines for How do I write a good answer?. From review.

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.