Convert user input to a list of integers! 💻🐍🚀 #learntocode #python #programming
Convert user input to a list of integers! 💻🐍🚀 #learntocode #python #programming

## Data Science and Machine Learning Internship …

• 22k Enrolled Learners
• Weekend/Weekday
• Live Class

Sometimes while coding in Python, you will need to take 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 which is derived from File Handling which is further explained by Edureka’s in-house Python trainers in the Python Certification Course.

Python Full Course – Learn Python in 12 Hours | Python Tutorial For Beginners | Edureka

This Edureka Python Full Course helps you to became a master in basic and advanced level of Python Programming Concepts.

As you might already know, in order to accept an 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

Find out our Python Training in Top Cities/Countries

 India USA Other Cities/Countries Bangalore New York UK Hyderabad Chicago London Delhi Atlanta Canada Chennai Houston Toronto Mumbai Los Angeles Australia Pune Boston UAE Kolkata Miami Dubai Ahmedabad San Francisco Philippines

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.

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

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 comma: Julius,Mark,John

Printing all family member names

Juluis

Mark

John

Analysis

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

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

Examples

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

# 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): ele = int(input()) lst.append(ele) # adding the element print(lst)

Output

Let us take a look at the next example,

# 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

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

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?

To get in-depth knowledge on Python along with its various applications, you can enroll now for a live online Python Certification Course with 24/7 support and lifetime access.

Got a question for us? Mention them in the comments section of article and we will get back to you or join our Python Master course.

 Course Name Date Python Certification Training Course Class Starts on 23rd September,2023 23rd September SAT&SUN (Weekend Batch) View Details Python Certification Training Course Class Starts on 21st October,2023 21st October SAT&SUN (Weekend Batch) View Details

edureka.co

You are watching: How To Input a List in Python?. Info created by GBee English Center selection and synthesis along with other related topics.