Skip to content Skip to sidebar Skip to footer

Write a Program That Reads a Sequence of Integer Inputs in Python

How To Input a List in Python?

Last updated on Dec 21,2021 120.7K Views

36 / 62 Blog from Python Fundamentals

Sometimes while coding in Python, you will demand to have a list equally an input. While this might sound simple at beginning, information technology is often regarded as a complex job to achieve for a beginner. This article volition tell you how to input a list in Python.

Post-obit pointers will be covered in this commodity,

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

And so allow us become started then,

Input a List in Python

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

This how to input a List in Python article, will accost main areas of concern

Find out our Python Training in Top Cities/Countries

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

Accept a listing of number as an input in Python

Have a wait at the instance program below, which accepts a listing of numbers equally an input in Python.

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

When the in a higher place program is run, the output will expect something like this.

Output

Enter a list element separated by infinite two 4 half-dozen 9

Computing sum of element of input list

Sum =  20

Assay

Now let u.s.a. breakdown the program and see how the workings behind it.

  1. As yous already know, whenever nosotros use the input() function in Python, information technology converts the user input into a cord. Therefore in the above program, nosotros accepted a list element from a user in the form of a string which is separated by spaces.
  2. One thing to note here, is that you have the power to accept a cord separated by the operator comma (,) too. Only in this situation y'all need to make use of the split() office in gild to pass the argument equally well as the separator in the Python program.
  3. If you lot await closely, you will find that we have fabricated use of the function input_string.divide() 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 summate its sum.

Moving on to the adjacent topic of this article let u.s.a. see  how to input a list in python that holds strings,

Accept a List of Strings from the User

Similar to the above program, nosotros have the ability to create a program in Python to have a listing of strings from the user. Take a look at the example below to understand this ameliorate.

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

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

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

Printing all family unit member names

Juluis

Mark

John

Analysis

Allow the states breakdown the above program into pointers and understand it better.

  1. Similar to the before case, we accepted an input list from the user in the course of a string separated by commas.
  2. We have made employ 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, every bit you tin can see in the output shared in a higher place.

Moving on let the states encounter how this concept fares from programming perspective,

Examples

Let u.s. take a look at a couple of other examples to sympathize how to input a list in Python.

Case 1

# creating an empty listing lst = [] # number of elemetns every bit 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

Output - Input A List In Python - Edureka

Let us have a look at the next example,

Example 2

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

Output - Input A List In Python - Edureka

Instance 3

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

This would exist the concluding example of this article,

Example 4

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

Output

Output - Input A List In Python - Edureka

This is it guys this brings us to the end of this article on  How To Input A List In Python?

To become in-depth noesis on Python along with its various applications, you can enroll now for live online Python training with 24/vii support and lifetime access.

Got a question for us? Mention them in the comments section of  article and we will become dorsum to you or bring together our Python Master course.

waitswevoing.blogspot.com

Source: https://www.edureka.co/blog/input-a-list-in-python/

Post a Comment for "Write a Program That Reads a Sequence of Integer Inputs in Python"