• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / How to Get a List as User Input in Python

How to Get a List as User Input in Python

Author: Aditya Raj
Last Updated: December 15, 2021

We can take a value as input from the user using the input() function. What if we have to get a list of values as input? In this article, we will discuss two ways to get a list as  user input in Python.

Get A List As User Input Using The For Loop

We can get a list of values using the for loop in python. For this, we can first create an empty list and a count variable. After that, we will  ask the user for the total count of values in the list. After getting the total count of values, we can run the input() function count number of times using the for loop and add the input to the list using the append() method as follows.

input_count = int(input("Input the Total number of elements in the list:"))
input_list = list()
for i in range(input_count):
    input_value = input("Enter value {}:".format(i + 1))
    input_list.append(input_value)
print("The list given as input by the user is :", input_list)

Output:

Input the Total number of elements in the list:5
Enter value 1:12
Enter value 2:345
Enter value 3:PFB
Enter value 4:12345
Enter value 5:Aditya
The list given as input by the user is : ['12', '345', 'PFB', '12345', 'Aditya']

Get A List As User Input Using The While Loop

Instead of asking the user for total count of input values, we can ask the user to give a special value as input when they want to stop giving values as input. After that, we can keep taking inputs until the user gives the specific value as input signalling that there are no more values left. We can do this using the while loop in python.

In this method, we will first create an empty list and a boolean variable flag. We will initialize the flag to True. Here, flag will work as the decision variable in the while loop. After that we will start taking inputs from users in the while loop and will append them to the list. If the user inputs the specific value signalling that there are no more values left, we will assign False to the flag variable. When the flag’s value turns False, it forces the while loop to terminate.

After the while loop stops execution, we get the list of all the values given as input as seen in the below example. 

flag = True
input_list = list()
while flag:
    input_value = input("Enter the value in the list. To finish, press enter key without any input:\n")
    if input_value == "":
        flag = False
        continue
    input_list.append(input_value)
print("The list given as input by the user is :", input_list)

Output:

Enter the value in the list. To finish, press enter key without any input:
12
Enter the value in the list. To finish, press enter key without any input:
23
Enter the value in the list. To finish, press enter key without any input:
Aditya
Enter the value in the list. To finish, press enter key without any input:
567
Enter the value in the list. To finish, press enter key without any input:

The list given as input by the user is : ['12', '23', 'Aditya', '567']

Get A List As User Input By Using  The input() Method Only Once

We know that taking user input is costly in terms of time and resource as the program has to make system calls while taking inputs from the user. So, to maximize the efficiency of the program, we can avoid multiple use of the input() function while taking a list as user input.

For this, we will ask the user to input all the values in the list by separating them using space characters. After taking the space separated values as input, we will use the python string split operation to get a list of all the input values. This can be observed in the following example.

input_values = input("Enter the values in the list separated by space:\n")
input_list = input_values.split()
print("The list given as input by the user is :", input_list)

Output:

Enter the values in the list separated by space:
12 345 Aditya PFB 123345
The list given as input by the user is : ['12', '345', 'Aditya', 'PFB', '123345']

Conclusion

In this article, we have seen different ways to get a list as user input in python. To read about taking inputs from a file in Python, you can read this article on file handling in Python.

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup bitly Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehensions in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Pandas Append Row to DataFrame
  • Convert String to DataFrame in Python
  • Pandas DataFrame to List in Python
  • Solved: Dataframe Constructor Not Properly Called Error in Pandas
  • Overwrite a File in Python

Copyright © 2012–2023 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us