10

Just a quick question.
Suppose, I have a simple for-loop like

for i in range(1,11):
    x = raw_input() 

and I want to store all the values of x that I will be getting throughout the loop in different variables such that I can use all these different variables later on when the loop is over.

5
  • 1
    Why are all of the valid answers here getting downvotes? Commented Sep 7, 2016 at 9:45
  • 1
    Even I can't understand that. I'm not doing it. Commented Sep 7, 2016 at 10:05
  • Looks like the downvotes seem to have now been removed, meh, I'm not complaining. Commented Sep 7, 2016 at 10:55
  • 1
    Every downvotes are removed, except the one in my answer. Can't just figure out what have I done wrong. Poor funny downvoter, didn't have time to explain the reason :D Commented Sep 7, 2016 at 11:00
  • is raw_input() function you've created? Commented Apr 14, 2019 at 18:52

4 Answers 4

14

Create a list before the loop and store x in the list as you iterate:

l=[]
for i in range(1,11):
    x = raw_input()
    l.append(x)
print(l)
Sign up to request clarification or add additional context in comments.

1 Comment

It worked in my problem here: stackoverflow.com/questions/71572659/… Thank you.
7

You can store each input in a list, then access them later when you want.

inputs = []
for i in range(1,11);
    x = raw_input()
    inputs.append(x)


# print all inputs
for inp in inputs:
    print(inp)

# Access a specific input
print(inp[0])
print(inp[1])

Comments

5

You can form a list with them.

your_list = [raw_input() for _ in range(1, 11)]

To print the list, do:

print your_list

To iterate through the list, do:

for i in your_list:
    #do_something

1 Comment

Even I don't get why such helpful responses are getting a down vote.
-1

using Python 3:

m=[]
for i in range(1,11):
    m.append(i**2) #your expression
print(m)

#or
m=[i**2 for i in range(1,11)]
print(m)

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.