Open In App

How to Take Multiple Inputs Using Loop in Python

Last Updated : 22 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Taking multiple inputs in Python is a common task, especially when dealing with user interactions or processing data sets. Using a for loop can simplify the process and make the code more efficient. In this article, we will explore simple and commonly used methods to take multiple inputs in Python using loops in Python.

Take Multiple Inputs Using Loop in Python

Below, are some of the ways to take multiple inputs In Python Using Loop:

Take Multiple Inputs In Python Using a List and For Loop

In this example, below Python code takes a user-specified number of inputs, iterates through a for loop, and prompts the user to enter each input sequentially. The entered values are then stored in a list called `user_inputs`, and the final list is printed to display all user inputs. The use of a for loop simplifies the process of collecting multiple inputs in an organized manner.

Python3




#  Using a List and For Loop
num_inputs = int(input("Enter the number of inputs: "))
user_inputs = []
 
for i in range(num_inputs):
    user_input = input(f"Enter input {i + 1}: ")
    user_inputs.append(user_input)
 
print("User inputs:", user_inputs)


Output:

Enter the number of inputs: 2
Enter input 1: 11
Enter input 2: 12
User inputs: ['11', '12']

Take Multiple Inputs In Python Using List Comprehension

In this example, below Python code employs list comprehension to efficiently collect multiple user inputs based on the specified number. It prompts the user to enter each input within the specified range, creating a list called `user_inputs`. Finally, the program prints the collected user inputs for easy verification and display.

Python3




# Using List Comprehension
num_inputs = int(input("Enter the number of inputs: "))
user_inputs = [input(f"Enter input {i + 1}: ") for i in range(num_inputs)]
 
print("User inputs:", user_inputs)


Output:

Enter the number of inputs: 2
Enter input 1: 11
Enter input 2: 2
User inputs: ['11', '2']

Take Multiple Inputs In Python Using While Loop

In this example, below code initializes an empty list called `inputs_list` to store user inputs. It then prompts the user to enter a value and uses a `while` loop to continuously take inputs until the user types ‘exit’. Each entered value is appended to the `inputs_list`. Finally, the program prints the collected inputs after the loop exits.

Python3




# Initialize an empty list to store inputs
inputs_list = []
 
# Set the initial value for the input
user_input = input("Enter a value (type 'exit' to stop): ")
 
# Use a while loop to continue taking inputs until 'exit' is entered
while user_input.lower() != 'exit':
    inputs_list.append(user_input)
    user_input = input("Enter another value (type 'exit' to stop): ")
 
# Display the collected inputs
print("Collected Inputs:", inputs_list)


Output

Enter a value (type 'exit' to stop): 1
Enter another value (type 'exit' to stop): 11
Enter another value (type 'exit' to stop): 12
Enter another value (type 'exit' to stop): exit
Collected Inputs: ['1', '11', '12']

Conclusion

Taking multiple inputs in Python using a for loop can be achieved through various methods, each offering its own advantages. Whether you prefer using a list, list comprehension, map and split, or a generator expression, these approaches provide flexibility and readability to your code. Choose the method that best suits your specific use case and coding style, and enjoy the efficiency and simplicity of handling multiple inputs in Python.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads