List in Python or python lists
Introduction
Python, a versatile and powerful programming language, is widely used for its simplicity and ease of learning. One of its fundamental data structures is the list, which allows you to store a collection of elements in a single variable. In this article, we will explore various methods of inputting lists in Python, along with detailed explanations
Understanding Lists in Python
Lists are ordered collections of items, and each item within a list is referred to as an element. Lists can contain elements of different data types, such as integers, strings, or even other lists. The flexibility of lists makes them a fundamental tool for various programming tasks.
We often encounter a situation when we need to take number/string as input from the user. now, we will see how to get as input a list from the user or list indexing in python.Â
Examples:
Input : n = 4, ele = 1 2 3 4 Output : [1, 2, 3, 4] Input : n = 6, ele = 3 4 1 7 9 6 Output : [3, 4, 1, 7, 9, 6]
Code #1:Â Manual Input
The simplest way to create a list is by manually inputting its elements. Here’s a step-by-step guide:
- Begin by defining an empty list. For instance:
my_list = []
2. Add elements to the list using the append()
method. For example:
my_list.append(42)
my_list.append('hello')
my_list.append([1, 2, 3])
Code #2:Â Basic exampleÂ
# creating an empty list lst = [] # number of elements as input n = int(input("Enter number of elements : ")) # iterating till the range for i in range(0, n): ele = int(input()) lst.append(ele) # adding the element print(lst)
Output:
Code #3:Â With handling exceptionÂ
- Python3
# try block to handle the exception try: my_list = [] while True: my_list.append(int(input())) # if the input is not-integer, just print the list except: print(my_list)
Output:
Code #4:Â Using map()Â
Another approach to creating a list from user input is by using map()
in combination. This method converts the input to a list of integers, for example:
- Python3
# number of elements n = int(input("Enter number of elements : ")) # Below line read inputs from user using map() function a = list(map(int,input("\nEnter the numbers : ").strip().split()))[:n] print("\nList is - ", a)
Output:
Code #5:Â List of lists as inputÂ
- Python3
lst = [ ] n = int(input("Enter number of elements : ")) for i in range(0, n): ele = [input(), int(input())] lst.append(ele) print(lst)
Output:
Code #6:Â Using List Comprehension and TypecastingÂ
List comprehension is a concise and elegant way to create lists in Python. It allows you to define lists in a single line using a compact syntax. Here’s how you can do it:
- Python3
# For list of integers lst1 = [] # For list of strings/chars lst2 = [] lst1 = [int(item) for item in input("Enter the list items : ").split()] lst2 = [item for item in input("Enter the list items : ").split()] print(lst1) print(lst2)
Output:
Code #7:Â Splitting a String into a List
You can split a string into a list of substrings based on a delimiter. This is particularly useful when dealing with input from the user. Here’s how to do it:
- Use the
split()
method on the input string to obtain a list. For instance:
input_string = "apple orange banana"
fruits_list = input_string.split()
Code #8:Â Converting a Range to a List
Python provides a built-in function called list()
that can convert a range of numbers into a list. This method is useful when you need to generate a sequence of integers within a specific range. Here’s how to use it:
- Create a range using the
range()
function. For example, to generate a list of numbers from 1 to 10:
num_range = range(1, 11)
2. Convert the range to a list using the list()
function:
num_list = list(num_range)
Conclusion
In this article, we covered various methods to input lists in Python, including manual input, list comprehension, splitting strings, and using map()
and split()
together. Depending on your specific use case, each method offers a convenient way to create and manipulate lists efficiently. Python’s versatility and user-friendly syntax make it a top choice for developers when working with lists and other data structures. Happy coding!