How to Take a List as Input in Python Without Specifying Size? Last Updated : 19 Nov, 2024 Comments Improve Suggest changes Like Article Like Report In many situations, we might want to take list as input without knowing the size in Python beforehand. This approach provides flexibility by allowing users to input as many elements as they want until a specified condition (like pressing Enter) is met.Let’s start with the most simple method to take a list as input without specifying the size.The simplest way to take input for a list without specifying its size is by using the split() method. This method is ideal for cases where all inputs are given in a single line separated by whitespace. Python # Take input in one line a = input("Enter elements separated by spaces: ").split() print(a) Output:Enter elements separated by spaces: 26 27 28['26', '27', '28']split() method splits the input string into individual elements based on whitespace and stores them in the list a.Let's explore other methods to take list as input without knowing size:Table of ContentUsing a While LoopUsing List Comprehension with input()Using a While LoopOne Basic way to create a list without knowing its size in advance is by using a while loop that continues until a specific condition is met. This method is flexible as it allows users to input elements one at a time. Python # Initialize an empty list a = [] print("Enter elements (press Enter without typing to stop):") while True: val = input("Enter an element (or press Enter to finish): ") if val == "": break # Exit loop when user presses Enter without typing a.append(el) print(a) Output:Enter elements (press Enter without typing to stop):Enter an element (or press Enter to finish): 23Enter an element (or press Enter to finish): 45Enter an element (or press Enter to finish): 54Enter an element (or press Enter to finish): ['23', '45', '54']Using List Comprehension with input()List comprehension can be used with input() to take list input dynamically without specifying size. Python # Read input until the user stops a = [i for i in iter(lambda: input("Enter an element (or press Enter to stop): "), "")] print(a) Output:Enter an element (or press Enter to stop): 12 16 19Enter an element (or press Enter to stop): 12Enter an element (or press Enter to stop): ['12 16 19', '12']Explanation: This method uses iter() to continuously take input until an empty string is entered, indicating the stopping condition. Comment More infoAdvertise with us Next Article How to Take a List as Input in Python Without Specifying Size? A anuragtriarna Follow Improve Article Tags : Python Python Programs python-list Python list-programs Practice Tags : pythonpython-list Similar Reads How to Take List of Tuples as Input in Python? Lists of tuples are useful data structures in Python and commonly used when you need to group related elements together while maintaining immutability within each group. We may need to take input for a list of tuples from the user. This article will explore different methods to take a list of tuples 3 min read Initialize a List of Any Size with Specific Values in Python In Python, we often need to create a list with a specific size and initialize all elements with the same value. This is useful when we want to set up a list but don't yet have the exact values we need. There are several ways to do this.Letâs start with the simplest way to create a list with repeated 2 min read How to take string as input from a user in Python Accepting input is straightforward and very user-friendly in Python because of the built-in input() function. In this article, weâll walk through how to take string input from a user in Python with simple examples. The input() function allows us to prompt the user for input and read it as a string. 3 min read How to Input a List in Python using For Loop Using a for loop to take list input is a simple and common method. It allows users to enter multiple values one by one, storing them in a list. This approach is flexible and works well when the number of inputs is known in advance.Letâs start with a basic way to input a list using a for loop in Pyth 2 min read How to Take Array Input in Python Using NumPy NumPy is a powerful library in Python used for numerical computing. It provides an efficient way to work with arrays making operations on large datasets faster and easier. To take input for arrays in NumPy, you can use numpy.array. Taking Array Input Using numpy.array()The most simple way to create 3 min read List As Input in Python in Single Line Python provides several ways to take a list as input in Python in a single line. Taking user input is a common task in Python programming, and when it comes to handling lists, there are several efficient ways to accomplish this in just a single line of code. In this article, we will explore four com 3 min read Get Length of a List in Python Without Using Len() Python len() method is the most common and widely used method for getting the length of a list in Python. But we can use other methods as well for getting the length or size of the list. In this article, we will see how to find the length of a list in Python without using the len() function. Find Th 2 min read How to Add Floats to a List in Python Adding floats to a list in Python is simple and can be done in several ways. The easiest way to add a float to a list is by using the append() method. This method adds a single value to the end of the list.Pythona = [1.2, 3.4, 5.6] #Add a float value (7.8) to the end of the list a.append(7.8) print( 2 min read How To Find the Length of a List in Python The length of a list refers to the number of elements in the list. There are several methods to determine the length of a list in Python. For example, consider a list l = [1, 2, 3, 4, 5], length of this list is 5 as it contains 5 elements in it. Let's explore different methods to find the length of 2 min read How to Remove All Items From a List in Python Removing all items from the list can be done by using various different methods in Python. Using clear()clear() method is the simplest and most efficient way to remove all items from a list.Pythona = [1, 2, 3, 4, 5] # Remove all items using clear() a.clear() # Print the empty list print(a) Output[] 2 min read Like