How to input list in python

Python Get a list as input from user

In this article we will see you how to ask the user to enter elements of a list and finally create the list with those entered values.

With format and input

The format function can be used to fill in the values in the place holders and the input function will capture the value entered by the user. Finally, we will append the elements into the list one by one.

Example

listA = [] # Input number of elemetns n = int(input("Enter number of elements in the list : ")) # iterating till the range for i in range(0, n): print("Enter element No-<>: ".format(i+1)) elm = int(input()) listA.append(elm) # adding the element print("The entered list is: \n",listA)

Output

Running the above code gives us the following result −

Enter number of elements in the list : 4 Enter element No-1: 7 Enter element No-2: 45 Enter element No-3: 1 Enter element No-4: 74 The entered list is: [7, 45, 1, 74]

With map

Another approach is to ask the user to enter the values continuously but separated by comma. Here we use the map function together the inputs into a list.

Читайте также:  Center text on image php

Example

listA = [] # Input number of elemetns n = int(input("Enter number of elements in the list : ")) # Enter elements separated by comma listA = list(map(int,input("Enter the numbers : ").strip().split(',')))[:n] print("The entered list is: \n",listA)

Output

Running the above code gives us the following result −

Enter number of elements in the list : 4 Enter the numbers : 12,45,65,32 The entered list is: [12, 45, 65, 32]

Entering list of lists

We can also use input function twice so that we can create a list of lists. Use the range function to keep account on number of elements to be entered and the format function to enter the elements one by one. Finally, we append each entered element to the newly created list.

Example

listA = [] # Input number of elemetns n = int(input("Enter number of elements in the list : ")) # Each sublist has two elements for i in range(0, n): print("Enter element No-<>: ".format(i + 1)) ele = [input(), int(input())] listA.append(ele) print("The entered list is: \n",listA)

Output

Running the above code gives us the following result −

Enter number of elements in the list : 2 Enter element No-1: 'Mon' 3 Enter element No-2: 'Tue' 4 The entered list is: [["'Mon'", 3], ["'Tue'", 4]]

Источник

Python Take list as an input from a user

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

How to take a list as input in Python

Accept list as an input from a user 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.

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 wp-block-preformatted">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.

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

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

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

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

Источник

Python Accept List as a input From User

To take input in list from user in python; In this python article, we would share with you how to declare a list, input elements, append the elements in the list, and ultimately, print the list in python.

When you create this program in Python, you will need Python’s functions like, list.append() function, input() function, for loop with range() function.

Python Program to Take Input in List from User

  • First of all, declare a list in python.
  • Take Input a limit of the list from the user.
  • Use for loop to take a single input number from the user.
  • Use list.append() to add elements in python list.
  • Print the list using for loop.
# Python Program to input, append and print the list elements # declare a list myList = [] # take number from user, how many element in list n = int (input ("How many element in list :- ")) # append element into list using list.append for i in range (n) : storeElement = int (input ("Enter an integer num :- ")) myList.append (storeElement) # print all elements print("Input list elements are: ") for i in range (n) : print(myList [i])

After executing a the program, the output will be:

How many element in list :- 4 Enter an integer num :- 1 Enter an integer num :- 2 Enter an integer num :- 3 Enter an integer num :- 4 Input list elements are: 1 2 3 4
  1. Python Print List Elements in Different Way
  2. Python Add and Remove Elements From List
  3. Python: Add/Insert Element at Specified Index in List
  4. Python Program to Remove ith/Nth Occurrence of Given Word in List
  5. Python Program to Sort List in Ascending and Descending Order
  6. Python to Find the Differences of Two Lists
  7. Python to Find Minimum and Maximum Elements of List
  8. Python Programs to Split Even and Odd Numbers in Separate List
  9. Python Program to Create Two Lists with First Half and Second Half Elements of Given List
  10. Python Program to Swap Two Elements in a List
  11. Python Program to Reverse List
  12. How To Select Random Item From A List In Python
  13. Python Program to Find Second Largest Number in List

Author Admin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Источник

Оцените статью