How To Input a List in Python?

Wajiha Urooj
Edureka
Published in
5 min readSep 27, 2019

Sometimes while coding in Python, you will need to make a list as an input. While this might sound simple at first, it is often regarded as a complex task to accomplish for a beginner. This article will tell you how to input a list in Python.

The following pointers will be covered in this article,

  • Input a List in Python
  • Accept a list of number as an input in Python
  • Accept a List of Strings from the User
  • Examples

So let us get started then,

Input a List in Python

As you might already know, in order to accept input from the user in Python, we can make use of the input() function. When used, it enables the programmer to accept either a string, integer, or even a character as an input from the user. But when it comes to accepting a list as an input, the approach we follow is slightly different.

This how-to input a List in Python article will address main areas of concern

Accept a list of number as an input in Python

Take a look at the example program below, which accepts a list of numbers as an input in Python.

input_string = input("Enter a list element separated by space ")
list = input_string.split()
print("Calculating sum of element of input list")
sum = 0
for num in list:
sum += int (num)
print("Sum = ",sum)

When the above program is run, the output will look something like this.

Output
Enter a list element separated by space 2 4 6 9

Calculating sum of element of input list

Sum = 20

Analysis

Now let us breakdown the program and see how the workings behind it.

  1. As you already know, whenever we use the input() function in Python, it converts the user input into a string. Therefore in the above program, we accepted a list element from a user in the form of a string that is separated by spaces.
  2. One thing to note here is that you have the ability to accept a string separated by the operator comma (,) as well. But in this situation, you need to make use of the split() function in order to pass the argument as well as the separator in the Python program.
  3. If you look closely, you will find that we have made use of the function input_string.split() to split the input string separated by spaces from the user and converted them into individual elements to be added into a list.
  4. We have also made use of the For loop and converted every element into an integer to calculate its sum.

Moving on to the next topic of this article let us see how to input a list in python that holds strings,

Accept a List of Strings from the User

Similar to the above program, we have the ability to create a program in Python to accept a list of strings from the user. Take a look at the example below to understand this better.

input_string = input("Enter family members separated by comma ")
family_list = input_string.split(",")
print("Printing all family member names")
for name in family_list:
print(name)

When the above program is run, the output will look something like this.

Enter family members separated by a comma: Julius,Mark,John

Printing all family member names

Julius

Mark

John

Analysis

Let us breakdown the above program into pointers and understand it better.

  1. Similar to the earlier example, we accepted an input list from the user in the form of a string separated by commas.
  2. We have made use of the input_string.split(“,”) function to split the string separated by commas and convert it into a list of string to be used in the program.
  3. We used a for loop and printed out all family names in order, as you can see in the output shared above.

Moving on let us see how this concept fares from a programming perspective,

Examples

Let us take a look at a couple of other examples to understand how to input a list in Python.

Example 1

# creating an empty list
lst = []
# number of elemetns as input
n = int(input("Enter number of elements : "))
# iterating till the range
for i in range(0, n):
else = int(input())
lst.append(ele) # adding the element
print(lst)

Output

Let us take a look at the next example,

Example 2

# try block to handle the exception
try:
my_list = []
while True:
my_list.append(int(input()))
# if input is not-integer, just print the list
except:
print(my_list)

Output

Example 3

# number of elements
n = int(input("Enter number of elements : "))
# Below line read inputs from user using map() function
a = list(map(int,input("nEnter the numbers : ").strip().split()))[:n]
print("nList is - ", a)

This would be the final example of this article,

Example 4

lst = [ ]
n = int(input("Enter number of elements : "))
for i in range(0, n):
ele = [input(), int(input())]
lst.append(ele)
print(lst)

Output

This is it guys this brings us to the end of this article on How To Input A List In Python? If you wish to check out more articles on the market’s most trending technologies like Artificial Intelligence, DevOps, Ethical Hacking, then you can refer to Edureka’s official site.

Do look out for other articles in this series which will explain the various other aspects of Python and Data Science.

1. Machine Learning Classifier in Python

2. Python Scikit-Learn Cheat Sheet

3. Machine Learning Tools

4. Python Libraries For Data Science And Machine Learning

5. Chatbot In Python

6. Python Collections

7. Python Modules

8. Python developer Skills

9. OOPs Interview Questions and Answers

10. Resume For A Python Developer

11. Exploratory Data Analysis In Python

12. Snake Game With Python’s Turtle Module

13. Python Developer Salary

14. Principal Component Analysis

15. Python vs C++

16. Scrapy Tutorial

17. Python SciPy

18. Least Squares Regression Method

19. Jupyter Notebook Cheat Sheet

20. Python Basics

21. Python Pattern Programs

22. Generators in Python

23. Python Decorator

24. What is Socket Programming in Python

25. Mobile Applications Using Kivy In Python

26. Top 10 Best Books To Learn & Practice Python

27. Robot Framework With Python

28. Snake Game in Python using PyGame

29. Django Interview Questions and Answers

30. Top 10 Python Applications

31. Hash Tables and Hashmaps in Python

32. Python 3.8

33. Support Vector Machine

34. Python Tutorial

35. Python Spyder

Originally published at https://www.edureka.co on September 27, 2019.

--

--