- Different Methodologies in Project Management
- Difference between Soap and Rest | Know more about it
- What is Python array? Learn with examples
- Best Career options after Engineering | Everything You Need to Know [OverView]
- What is list in Python ? All you need to know [ OverView ]
- What is Data Modelling? : All you need to know [ OverView ]
- What are Microservices? : A Complete Guide For Beginners with Best Practices
- What is Python Programming | A Definitive Guide with Best Practices
- All You Need To Know About Python List | A Complete Guide For Beginners with Best Practices
- What Is a Software Developer | Software Developer job description and duties | Everything You Need to Know
- What is Artificial Intelligence Chatbot?
- Kotlin vs Java | Know Their Differences and Which Should You Learn?
- What is Abstraction in Java | Implementations of Abstraction in Java | A Definitive Guide with Best Practices
- What are the Important Data Structures and Algorithms in Python?
- Go vs Python | Know Their Differences and Which Should You Learn?
- Best Python IDEs and Code Editors | Expert’s Top Picks | Everything You Need to Know
- Go Programming Language | Expert’s Top Picks | A Definitive Guide [ OverView ]
- Python Scopes and Their Built-in Functions | Everything You Need to Know | Expert’s Top Picks
- Python String Formatting | A Complete Guide For Beginners [ OverView ]
- Python Serialization | A Complete Guide For Beginners | Learning Guide
- What is .Net FrameWork? Uses and its Benefits | Everything You Need to Know
- What is Quality Assurance ? : A Definitive Guide | Everything You Need to Know [ OverView ]
- What is Spike Testing ? : A Definitive Guide | Expert’s Top Picks | Free Guide Tutorial
- Average Full Stack Developer Salary in India [ For Freshers and Experience ]
- What is WSDL in Web Services ? Expert’s Top Picks | Free Guide Tutorial
- Virtual Instrumentation using Labview | Comprehensive Guide [ Explained ]
- Gradle vs Maven | Know Their Differences and Which Should You Learn?
- Python Sleep Method | Free Guide Tutorial & REAL-TIME Examples
- Kotlin vs Python | A Complete Guide with Best Practices
- Spring Boot vs Spring MVC | Differences and Which Should You Learn?
- IT Engineer Salary in India – How much does one earn?
- What is pip ? and Getting Started with Python pip | All you need to know [ OverView ]
- Node.js Installation on Windows and Ubuntu | Free Guide Tutorial
- Skills Needed for Full Stack Developers | All you need to know [ OverView ]
- What is Axios in React? and Its Uses [ OverView ]
- What is MEAN Stack? All you need to know [ OverView ]
- How to Install Node.JS on Ubuntu | Everything You Need to Know
- Average Annual Salary of a Python Certified Professional – Career Path
- What is Scala Programming? A Complete Guide with Best Practices
- What Is User Input in Python? Expert’s Top Picks
- Interface vs Abstract Class | Difference You Should Know
- Final Year Computer Science Project Ideas | All you need to know [ OverView ]
- Technical Architect | Free Guide Tutorial & REAL-TIME Examples
- Logical Programs in Java | Step-By-Step Process
- C++ vs Java | Difference You Should Know
- What is C Programming? Comprehensive Guide
- What Is a Quality Engineer? ( Everything You Need to Know )
- Python Project Ideas for Beginners | All you need to know
- How to Run Python Scripts? Comprehensive Guide
- Python Operators
- How To Install NumPy in Python?
- Top Software Courses to Get High Paying Jobs
- Loops In Python
- Tips to Avoid Application Rejection
- Top Young App Developers Who Became Millionaires
- Top Technical Courses After Graduation
- Node JS Architecture
- What is PyCharm?
- Resources To Help You Learn Java Programming
- How to Become a Software Engineer?
- Best Programming Languages to Learn in 2020
- Scala vs Python
- How to Become a Full Stack Developer?
- The Most Popular Java Applications Used World-wide
- What is Java String?
- Full Stack Developer vs Front End Developer vs Back End Developer
- Python Collections
- Identifiers in Python
- Dynamic Method Dispatch in Java
- Hadoop Ecosystem
- Method Overloading in Python
- Convert Decimal To Binary In Python
- How To Make A Chatbot In Python?
- How to Input a List in Python?
- Hash Tables and Hashmaps in Python
- Top Python Framework’s
- Python Split Method with Example
- Python Enumerate
- Inheritance in Java
- Init in Python
- Goto Statement in Python
- Literals in Java
- Polymorphism in Oops
- Socket Programming in Python
- Object Class in Java
- Break, Continue, and Pass Statements in Python
- Exception Handling in Java
- Java BASIC Programs
- Different Methodologies in Project Management
- Difference between Soap and Rest | Know more about it
- What is Python array? Learn with examples
- Best Career options after Engineering | Everything You Need to Know [OverView]
- What is list in Python ? All you need to know [ OverView ]
- What is Data Modelling? : All you need to know [ OverView ]
- What are Microservices? : A Complete Guide For Beginners with Best Practices
- What is Python Programming | A Definitive Guide with Best Practices
- All You Need To Know About Python List | A Complete Guide For Beginners with Best Practices
- What Is a Software Developer | Software Developer job description and duties | Everything You Need to Know
- What is Artificial Intelligence Chatbot?
- Kotlin vs Java | Know Their Differences and Which Should You Learn?
- What is Abstraction in Java | Implementations of Abstraction in Java | A Definitive Guide with Best Practices
- What are the Important Data Structures and Algorithms in Python?
- Go vs Python | Know Their Differences and Which Should You Learn?
- Best Python IDEs and Code Editors | Expert’s Top Picks | Everything You Need to Know
- Go Programming Language | Expert’s Top Picks | A Definitive Guide [ OverView ]
- Python Scopes and Their Built-in Functions | Everything You Need to Know | Expert’s Top Picks
- Python String Formatting | A Complete Guide For Beginners [ OverView ]
- Python Serialization | A Complete Guide For Beginners | Learning Guide
- What is .Net FrameWork? Uses and its Benefits | Everything You Need to Know
- What is Quality Assurance ? : A Definitive Guide | Everything You Need to Know [ OverView ]
- What is Spike Testing ? : A Definitive Guide | Expert’s Top Picks | Free Guide Tutorial
- Average Full Stack Developer Salary in India [ For Freshers and Experience ]
- What is WSDL in Web Services ? Expert’s Top Picks | Free Guide Tutorial
- Virtual Instrumentation using Labview | Comprehensive Guide [ Explained ]
- Gradle vs Maven | Know Their Differences and Which Should You Learn?
- Python Sleep Method | Free Guide Tutorial & REAL-TIME Examples
- Kotlin vs Python | A Complete Guide with Best Practices
- Spring Boot vs Spring MVC | Differences and Which Should You Learn?
- IT Engineer Salary in India – How much does one earn?
- What is pip ? and Getting Started with Python pip | All you need to know [ OverView ]
- Node.js Installation on Windows and Ubuntu | Free Guide Tutorial
- Skills Needed for Full Stack Developers | All you need to know [ OverView ]
- What is Axios in React? and Its Uses [ OverView ]
- What is MEAN Stack? All you need to know [ OverView ]
- How to Install Node.JS on Ubuntu | Everything You Need to Know
- Average Annual Salary of a Python Certified Professional – Career Path
- What is Scala Programming? A Complete Guide with Best Practices
- What Is User Input in Python? Expert’s Top Picks
- Interface vs Abstract Class | Difference You Should Know
- Final Year Computer Science Project Ideas | All you need to know [ OverView ]
- Technical Architect | Free Guide Tutorial & REAL-TIME Examples
- Logical Programs in Java | Step-By-Step Process
- C++ vs Java | Difference You Should Know
- What is C Programming? Comprehensive Guide
- What Is a Quality Engineer? ( Everything You Need to Know )
- Python Project Ideas for Beginners | All you need to know
- How to Run Python Scripts? Comprehensive Guide
- Python Operators
- How To Install NumPy in Python?
- Top Software Courses to Get High Paying Jobs
- Loops In Python
- Tips to Avoid Application Rejection
- Top Young App Developers Who Became Millionaires
- Top Technical Courses After Graduation
- Node JS Architecture
- What is PyCharm?
- Resources To Help You Learn Java Programming
- How to Become a Software Engineer?
- Best Programming Languages to Learn in 2020
- Scala vs Python
- How to Become a Full Stack Developer?
- The Most Popular Java Applications Used World-wide
- What is Java String?
- Full Stack Developer vs Front End Developer vs Back End Developer
- Python Collections
- Identifiers in Python
- Dynamic Method Dispatch in Java
- Hadoop Ecosystem
- Method Overloading in Python
- Convert Decimal To Binary In Python
- How To Make A Chatbot In Python?
- How to Input a List in Python?
- Hash Tables and Hashmaps in Python
- Top Python Framework’s
- Python Split Method with Example
- Python Enumerate
- Inheritance in Java
- Init in Python
- Goto Statement in Python
- Literals in Java
- Polymorphism in Oops
- Socket Programming in Python
- Object Class in Java
- Break, Continue, and Pass Statements in Python
- Exception Handling in Java
- Java BASIC Programs

How to Input a List in Python?
Last updated on 25th Sep 2020, Artciles, Blog, Software Engineering
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.
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.
Subscribe For Free Demo
Error: Contact form not found.
Accept a list of number as an input in Python
Example
- 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)
Output
- Enter a list element separated by space 2 4 6 9
- Calculating sum of element of input list
- Sum = 20
Now, let us understand how this program works:
- 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.
Accept a List of Strings from the User
Example
- 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)
Output
- Enter family members separated by a comma: Arvindh,Nivaas
- Printing all family member names
- Arvindh
- Gaviya
Now, let us understand how this program works:
- 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 strings 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.
With format and input
The format function can be used to fill in the values in the place holders and the input function will capture the value entered by the user. Finally, we will append the elements into the list one by one.
Example
- listA = []
- # Input number of elemetns
- n = int(input(“Enter number of elements in the list : “))
- # iterating till the range
- for i in range(0, n):
- print(“Enter element No-{}: “.format(i+1))
- elm = int(input())
- listA.append(elm) # adding the element
- print(“The entered list is: \n”,listA)
Output
- Running the above code gives us the following result −
- Enter number of elements in the list : 4
- Enter element No-1:
- 7
- Enter element No-2:
- 45
- Enter element No-3:
- 1
- Enter element No-4:
- 74
- The entered list is:
- [7, 45, 1, 74]
With map
Another approach is to ask the user to enter the values continuously but separated by comma. Here we use the map function together the inputs into a list.
Example
- listA = []
- # Input number of elemetns
- n = int(input(“Enter number of elements in the list : “))
- # Enter elements separated by comma
- listA = list(map(int,input(“Enter the numbers : “).strip().split(‘,’)))[:n]
- print(“The entered list is: \n”,listA)
Output
- Running the above code gives us the following result −
- Enter number of elements in the list : 4
- Enter the numbers : 12,45,65,32
- The entered list is:
- [12, 45, 65, 32]
Entering list of lists
We can also use input function twice so that we can create a list of lists. Use the range function to keep account on number of elements to be entered and the format function to enter the elements one by one. Finally, we append each entered element to the newly created list.
Example
- listA = []
- # Input number of elemetns
- n = int(input(“Enter number of elements in the list : “))
- # Each sublist has two elements
- for i in range(0, n):
- print(“Enter element No-{}: “.format(i + 1))
- ele = [input(), int(input())]
- listA.append(ele)
- print(“The entered list is: \n”,listA)
Output
- Running the above code gives us the following result −
- Enter number of elements in the list : 2
- Enter element No-1:
- ‘Mon’
- 3
- Enter element No-2:
- ‘Tue’
- 4
- The entered list is:
- [[“‘Mon'”, 3], [“‘Tue'”, 4]]