PYnative

Python Programming

  • Learn Python
  • Exercises
  • Quizzes
  • Code Editor
  • Tricks
Home » Python » Python take a list as input from a user

Python take a list as input from a user

Updated on: September 8, 2023 | 52 Comments

In this lesson, You will learn how to get a list as an input in Python. Using the input() function, you can take a list as input from a user in Python.

Using the Python input() function, we can directly accept strings, numbers, and characters from the user. However, to take a list as input from a user, we need to perform some parsing on user input to convert it into a list. We will see this in the below steps and examples.

Table of contents

  • How to Get a list of numbers as input from a user in Python
    • Example: Get a list of numbers as input from a user and calculate the sum of it
  • Input a list using input() and range() function in Python
  • Input a list using a list comprehension in Python
  • Input a list using the map function
  • Get a list of strings as input from a user
  • Accept a nested list as input
  • Next Steps

How to Get a list of numbers as input from a user in Python

Below are the simple steps to input a list of numbers 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 the split() function of the string class

    Next, the split() method breaks an input string into a list. In Python, the split() method divides a string into multiple substrings based on a specified delimiter. If no delimiter is provided, it defaults to splitting on whitespace.

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

    Now, access list elements using a for loop and range() function.

  4. Convert each element of the list into a number

    At the end, convert each element of a list to an integer using an int() function. If you want a list of strings as input, skip this step.Accept list as an input from a user in Python

Example: 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 \n')
user_list = input_string.split()
print('string 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])

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

Output:

Enter elements of a list separated by space 
string list:  ['5', '10', '15', '20', '25', '30']
User list:  [5, 10, 15, 20, 25, 30]
Sum =  105

Note:

The Python input() function always converts the user input into a string and then returns it to the calling program. With those in mind, we converted each element into a number using an int() function.

If you want to accept a list with float numbers, you can use the float() function.

Also, Solve:

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

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

Let’s see how to accept a list as input without using the split() method.

  • First, create an empty list.
  • Next, accept a list size from the user (i.e., the number of elements in a list)
  • Next, run the loop till the size of a list using a for loop and range() function
  • Now, use the input() function to receive a number from a user.
  • At the end, add the current number to the list using the append() function

Example:

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 in Python

List comprehension is a concise way to create lists in Python. It’s a syntactic construct that offers a more readable and often shorter alternative to creating lists using loops.

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.
  • Get numbers from the user using the input() function.
  • Split the string on whitespace and convert each number to an integer using a int() function.
  • Append all those numbers to the list.

Example:

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

Python’s map() function is a built-in function that applies a given function to all the items in the input list (or any iterable).

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

  • First, decide the list size.
  • Next, accept numbers from the user separated by space
  • Next, use the map() function to wrap each user-entered number in it and convert it into an integer or float as per your need

Example:

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 input from a user

Accepting a list of strings from the user is very straightforward.

  • Accept the list of strings from a user in the format of a string separated by space.
  • Use the split() function on the input string to break a string into a list of words.

Example:

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. Let’s see how to accept the following list from a user.

Nested List: [[10, 20, 30], [40, 50, 60], [70, 80, 90]]

Example:

# accept nested list from user
list_size = int(input("Enter the number of sub list "))

print("\n")
final_list = [[int(input("Enter single number and press enter: ")) for _ in range(list_size)] for _ in range(list_size)]
print("List is", final_list)

Output:

Enter the number of sub list 3
Enter single number and press enter: 10
Enter single number and press enter: 20
Enter single number and press enter: 30
Enter single number and press enter: 40
Enter single number and press enter: 50
Enter single number and press enter: 60
Enter single number and press enter: 70
Enter single number and press enter: 80
Enter single number and press enter: 90

List is [[10, 20, 30], [40, 50, 60], [70, 80, 90]]

Next Steps

Let me know your comments and feedback in the section below.

Also, Solve:

  • Python input and output exercise
  • Python input and output quiz
  • Python exercise for beginners
  • Python Quiz for beginners

Filed Under: Python, Python Basics

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

TweetF  sharein  shareP  Pin

About Vishal

I’m Vishal Hule, Founder of PYnative.com. I am a Python developer, and I love to write articles to help students, developers, and learners. Follow me on Twitter

Related Tutorial Topics:

Python Python Basics

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 10 questions
  • Each Quiz contains 12-15 MCQ
Exercises
Quizzes

Posted In

Python Python Basics
TweetF  sharein  shareP  Pin

  Python Tutorials

  • Get Started with Python
  • Python Statements
  • Python Comments
  • Python Keywords
  • Python Variables
  • Python Operators
  • Python Data Types
  • Python Casting
  • Python Control Flow statements
  • Python For Loop
  • Python While Loop
  • Python Break and Continue
  • Python Nested Loops
  • Python Input and Output
  • Python range function
  • Check user input is String or Number
  • Accept List as a input from user
  • Python Numbers
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python Functions
  • Python Modules
  • Python isinstance()
  • Python Object-Oriented Programming
  • Python Exceptions
  • Python Exercise for Beginners
  • Python Quiz for Beginners

All Python Topics

Python Basics Python Exercises Python Quizzes Python File Handling Python OOP Python Date and Time Python Random Python Regex Python Pandas Python Databases Python MySQL Python PostgreSQL Python SQLite Python JSON

About PYnative

PYnative.com is for Python lovers. Here, You can get Tutorials, Exercises, and Quizzes to practice and improve your Python skills.

Explore Python

  • Learn Python
  • Python Basics
  • Python Databases
  • Python Exercises
  • Python Quizzes
  • Online Python Code Editor
  • Python Tricks

Follow Us

To get New Python Tutorials, Exercises, and Quizzes

  • Twitter
  • Facebook
  • Sitemap

Legal Stuff

  • About Us
  • Contact Us

We use cookies to improve your experience. While using PYnative, you agree to have read and accepted our Terms Of Use, Cookie Policy, and Privacy Policy.

Copyright © 2018–2023 pynative.com