PYnative

Python Programming

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

Python Take list as an input from a user

Updated on: March 18, 2021 | 52 Comments

In this lesson, You will learn how to input a list in Python.

Using the Python input() 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
    • Input a list using input() and range() function
    • Input a list using a list comprehension
    • Input a list using the map function
  • Get a list of strings as an input from a user
  • Accept a nested list as input
  • Next Steps

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 split() function to split an input string by space. The split() method splits a string into a list.

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

    Using a for loop and range() 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 int() function.
    If you want a list of strings as input then skip this step.Accept list as an input from a user 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 input() 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 int() function. If you want to accept a list with float numbers you can use the float() 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 split() 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 for loop and range() function
  • use the input() function to receive a number from a user
  • Add the current number to the list using the append() 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 int() 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 map() function to wrap each user-entered number in it and convert it into an int or float 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 split() 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.

[[10, 20, 30], [40, 50, 60], [70, 80, 90]]
# 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.

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

Founder of PYnative.com I am a Python developer and I love to write articles to help developers. Follow me on Twitter. All the best for your future Python endeavors!

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