How do you take user input from an array in Python?

Programs are written to solve a specific problem of a user. Thus, the program must be such which can interact with the user. This means that a program must take input from the user and perform the task accordingly on the input which user provides.

The method to take input is different for different datatypes. We’ll discuss how to take input for various datatypes as well as how to take array input from the user.

String Input

The input() method is used to take string input from the user.The user can enter numeric value as well but it will be treated as a string. The program can contain any logic or operation to be performed on the string entered by the user ,but in example, we’ll simply print the string which the user enters.

Example

print("Enter a string")
a=input()
print("The string entered by user is",a)

Output

Enter a string
TutorialsPoint
The string entered by user is TutorialsPoint

The above example upon execution, prints the message “Enter a string” on the output screen and lets the user enter something. When input() function executes, the program flow will be stopped until the user gives some input. After entering the string, the second print statement executes.

Integer Input

The integer input can be taken by just type casting the input received into input(). Thus, for taking integer input, we use int(input()) . Only numerical values can be entered by the user, else it throws an error.

Example

print("Enter a number")
a=int(input())
print("The number entered by user is",a)

Output

Enter a number
10
The number entered by user is 10

Float Input

The float input can be taken by type casting input received in input() .We’ll use float(input()) to take float input. The user can enter integer or float values but the value will be treated as float.

Example

print("Enter a number")
a=float(input())
print("The number entered by user is",a)

Output

Enter a number
2.5
The number entered by user is 2.5

Take Input as Array of Integers

We may at times, need to take an array as input from the user. There is no separate syntax for taking array input.

Example

print("Enter no. of elements")
a=int(input())
print("Enter",a,"integer elements")
array=[]
for i in range(a):
   array.append(int(input()))
print("Array entered by user is",array)

Output

Enter no. of elements
5
Enter 5 integer elements
1
2
3
4
5
Array entered by user is [1, 2, 3, 4, 5]

In the above example, the size of the array is taken as input from the user. Then the array is declared and using for loop, we take further elements input from the user and append those in the array.

You want this - enter N and then take N number of elements.I am considering your input case is just like this

5
2 3 6 6 5

have this in this way in python 3.x (for python 2.x use raw_input() instead if input())

Python 3

n = int(input())
arr = input()   # takes the whole line of n numbers
l = list(map(int,arr.split(' '))) # split those numbers with space( becomes ['2','3','6','6','5']) and then map every element into int (becomes [2,3,6,6,5])

Python 2

n = int(raw_input())
arr = raw_input()   # takes the whole line of n numbers
l = list(map(int,arr.split(' '))) # split those numbers with space( becomes ['2','3','6','6','5']) and then map every element into int (becomes [2,3,6,6,5])

Using the Python

Enter elements of a list separated by space 5 10 15 20 25 30

list: ['5', '10', '15', '20', '25', '30']
Sum =  105
3 function, we can accept a string, integer, and character input from a user. Now, let see how to get a list as input from a user.

Table of contents

Get a list of numbers as input from a user

How to take a list as input in Python

  1. Use an input() function

    Use an input() function to accept the list elements from a user in the format of a string separated by space.

  2. Use split() function of string class

    Next, use a

    Enter elements of a list separated by space 5 10 15 20 25 30
    
    list: ['5', '10', '15', '20', '25', '30']
    Sum =  105
    4 function to split an input string by space. The
    Enter elements of a list separated by space 5 10 15 20 25 30
    
    list: ['5', '10', '15', '20', '25', '30']
    Sum =  105
    4 method splits a string into a list.

  3. Use for loop and range() function to iterate a user list

    Using a

    Enter elements of a list separated by space 5 10 15 20 25 30
    
    list: ['5', '10', '15', '20', '25', '30']
    Sum =  105
    6 loop and
    Enter elements of a list separated by space 5 10 15 20 25 30
    
    list: ['5', '10', '15', '20', '25', '30']
    Sum =  105
    7 function, we can access each element of the list along with the index number.

  4. Convert each element of list into number

    Convert each list element to an integer using a

    Enter elements of a list separated by space 5 10 15 20 25 30
    
    list: ['5', '10', '15', '20', '25', '30']
    Sum =  105
    8 function.
    If you want a list of strings as input then skip this step.
    How do you take user input from an array in Python?
    How do you take user input from an array in Python?

Example 1: Get a list of numbers as input from a user and calculate the sum of it

input_string = input('Enter elements of a list separated by space ')
print("\n")
user_list = input_string.split()
# print list
print('list: ', user_list)

# convert each item to int type
for i in range(len(user_list)):
    # convert each item to int type
    user_list[i] = int(user_list[i])

# Calculating the sum of list elements
print("Sum = ", sum(user_list))

Output:

Enter elements of a list separated by space 5 10 15 20 25 30

list: ['5', '10', '15', '20', '25', '30']
Sum =  105

Note: Python

Enter elements of a list separated by space 5 10 15 20 25 30

list: ['5', '10', '15', '20', '25', '30']
Sum =  105
3 function always converts the user input into a string then returns it to the calling program. With those in mind, we converted each element into a number using an
Enter elements of a list separated by space 5 10 15 20 25 30

list: ['5', '10', '15', '20', '25', '30']
Sum =  105
8 function. If you want to accept a list with float numbers you can use the
number_list = []
n = int(input("Enter the list size "))

print("\n")
for i in range(0, n):
    print("Enter number at index", i, )
    item = int(input())
    number_list.append(item)
print("User list is ", number_list)
1 function.

Solve:

  • Python input and output exercise
  • Python input and output quiz

Input a list using input() and range() function

Let’s see how to accept Python list as an input without using the

Enter elements of a list separated by space 5 10 15 20 25 30

list: ['5', '10', '15', '20', '25', '30']
Sum =  105
4 method.

  • First, create an empty list.
  • Next, accept a list size from the user (i.e., the number of elements in a list)
  • Run loop till the size of a list using a
    Enter elements of a list separated by space 5 10 15 20 25 30
    
    list: ['5', '10', '15', '20', '25', '30']
    Sum =  105
    6 loop and
    number_list = []
    n = int(input("Enter the list size "))
    
    print("\n")
    for i in range(0, n):
        print("Enter number at index", i, )
        item = int(input())
        number_list.append(item)
    print("User list is ", number_list)
    
    4 function
  • use the
    Enter elements of a list separated by space 5 10 15 20 25 30
    
    list: ['5', '10', '15', '20', '25', '30']
    Sum =  105
    3 function to receive a number from a user
  • Add the current number to the list using the
    number_list = []
    n = int(input("Enter the list size "))
    
    print("\n")
    for i in range(0, n):
        print("Enter number at index", i, )
        item = int(input())
        number_list.append(item)
    print("User list is ", number_list)
    
    6 function
number_list = []
n = int(input("Enter the list size "))

print("\n")
for i in range(0, n):
    print("Enter number at index", i, )
    item = int(input())
    number_list.append(item)
print("User list is ", number_list)

Output:

Enter the list size 5
Enter number at index 0
5
Enter number at index 1
10
Enter number at index 2
15
Enter number at index 3
20
Enter number at index 4
25

User list is  [5, 10, 15, 20, 25]

Input a list using a list comprehension

List comprehension is a more straightforward method to create a list from an existing list. It is generally a list of iterables generated to include only the items that satisfy a condition.

Let’ see how to use the list Comprehension to get the list as an input from the user. First, decide the size of the list.

Next, use the list comprehension to do the following tasks

  • Get numbers from the user using the input() function.
  • Split it string on whitespace and convert each number to an integer using an
    Enter elements of a list separated by space 5 10 15 20 25 30
    
    list: ['5', '10', '15', '20', '25', '30']
    Sum =  105
    8 function.
  • Add all that numbers to the list.
n = int(input("Enter the size of the list "))
print("\n")
num_list = list(int(num) for num in input("Enter the list items separated by space ").strip().split())[:n]

print("User list: ", num_list)

Output:

Enter the size of the list 5
Enter the list items separated by space 2 4 6 8 10

User list:  [2, 4, 6, 8, 10]

Input a list using the map function

Let’ see how to use the map() function to get a list as an input from the user.

  • First, decide the list size.
  • Next, accept numbers from the user separated by space
  • Next, use the
    number_list = []
    n = int(input("Enter the list size "))
    
    print("\n")
    for i in range(0, n):
        print("Enter number at index", i, )
        item = int(input())
        number_list.append(item)
    print("User list is ", number_list)
    
    8 function to wrap each user-entered number in it and convert it into an
    number_list = []
    n = int(input("Enter the list size "))
    
    print("\n")
    for i in range(0, n):
        print("Enter number at index", i, )
        item = int(input())
        number_list.append(item)
    print("User list is ", number_list)
    
    9 or
    Enter the list size 5
    Enter number at index 0
    5
    Enter number at index 1
    10
    Enter number at index 2
    15
    Enter number at index 3
    20
    Enter number at index 4
    25
    
    User list is  [5, 10, 15, 20, 25]
    0 as per your need
n = int(input("Enter the size of list : "))
print("\n")
numList = list(map(float, input("Enter the list numbers separated by space ").strip().split()))[:n]
print("User List: ", numList)

Output:

Enter the size of list : 5
Enter the list numbers separated by space 2.5 5.5 7.5 10.5 12.5

User List:  [2.5, 5.5, 7.5, 10.5, 12.5]

Get a list of strings as an input from a user

Accept a string list from the user is very straightforward.

  • Accept the list of strings from a user in the format of a string separated by space.
  • Use
    Enter elements of a list separated by space 5 10 15 20 25 30
    
    list: ['5', '10', '15', '20', '25', '30']
    Sum =  105
    4 function on input string to splits a string into a list of words.
input_string = input("Enter all family members name separated by space  ")
# Split string into words
family_list = input_string.split(" ")

print("\n")
# Iterate a list
print("Printing all family member names")
for name in family_list:
    print(name)

Output:

Enter all family members name separated by space  Jessa Emma Scott Kelly Tom

Printing all family member names
Jessa
Emma
Scott
Kelly
Tom

Accept a nested list as input

In this example, Let’s see how to get evenly sized lists from the user. In simple words, Let’s see how to accept the following list of lists from a user.

How do you take user input from an array?

To take input of an array, we must ask the user about the length of the array. After that, we use a Java for loop to take the input from the user and the same for loop is also used for retrieving the elements from the array.

How do you take an input from a single line to an array in Python?

However, Python provides the two methods that help us to take multiple values or input in one line..
# Taking multiple inputs in a single line..
# and type casting using list() function..
x = list(map(int, input("Enter multiple values: "). split())).
print("List of students: ", x).

How can we get user input and store it in array?

To read data from user create a scanner class. Read the size of the array to be created from the user using nextInt() method. Create an array with the specified size. In the loop read the values from the user and store in the array created above.