close

How to store values retrieved from a loop in different variables in Python?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to store values retrieved from a loop in different variables in Python in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to store values retrieved from a loop in different variables in Python?

  1. How to store values retrieved from a loop in different variables in Python?

    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()

  2. store values retrieved from a loop in different variables in Python

    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()

Method 1

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)

Method 2

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])

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read