In this tutorial, we will learn about create array of 2 lists in Python with a coding example. In many situations, you might have to come up with this type of requirements.
I know you are here just because you are in need of this trick to create array of 2 lists that means user get a array which is made by combination of 2 list and values are always users input.
If you don’t know how to create array of 2 lists using string list and array, then you are at the right place. Because in this tutorial we will understand how we can create array of 2 lists using Python. And this method choose to save time and hustle.
Create array of 2 lists in Python
Let’s learn this with a easy coding example,
1. At first, Function Definition –
def get_list_from_input(prompt):
- The function
get_list_from_inputtakes one parameter,prompt, which is a string used to prompt the user for input.
2. Then User Input Function –
input_string = input(prompt)
input(prompt)displays the prompt message and waits for the user to type in a response.- The response is captured as a string in
input_string.
3. Then Processing Input –
return [int(x) for x in input_string.split()]
input_string.split()splits the input string into a list of substrings based on spaces. For example, if the user inputs"1 2 3", it will be split into["1", "2", "3"].[int(x) for x in input_string.split()]is a list comprehension that iterates over each substring, converts it to an integer usingint(x), and collects these integers into a new list.- Return Statement: The function returns the list of integers created from the input string.
4. After that, Getting List1 from the User –
list1 = get_list_from_input("Enter the elements of the first list separated by spaces: ")
Calling the Function:
get_list_from_input("Enter the elements of the first list separated by spaces: ")prompts the user to enter the elements of the first list. The entered values are converted into a list of integers and assigned tolist1.
5. Then Getting List2 from the User –
list2 = get_list_from_input("Enter the elements of the second list separated by spaces: ")
Calling the Function:
get_list_from_input("Enter the elements of the second list separated by spaces: ")does the same for the second list, assigning the result tolist2.
6. Next Creating the Array of Lists –
array_of_lists = [list1, list2]
Combining Lists:
array_of_listsis created as a list containinglist1andlist2. So,array_of_listsis a nested list where the first element islist1and the second element islist2.
7. Finally, Printing the Result Section –
print("Array of lists:", array_of_lists)
- This line prints the message
"Array of lists:"followed by the contents ofarray_of_lists. - The printed output will show the two lists as a nested list.
Python Code – “Create array of 2 lists in Python”
def get_list_from_input(prompt):
# Prompt the user for input
input_string = input(prompt)
# Convert the input string into a list of integers
# Split the input string by spaces and convert each part to an integer
return [int(x) for x in input_string.split()]
# Get two lists from the user
list1 = get_list_from_input("Enter the elements of the first list separated by spaces: ")
list2 = get_list_from_input("Enter the elements of the second list separated by spaces: ")
# Create the array of lists
array_of_lists = [list1, list2]
# Print the result
print("Array of lists:", array_of_lists)
Let’s take an Input,
If the user enter First List – 15 5 2 6 7 888 9 3
If the user enter Second List – 5 4 7 9 31 78 5 2 5
Then the Output :
Enter the elements of the first list separated by spaces: 15 5 2 6 7 888 9 3 Enter the elements of the second list separated by spaces: 5 4 7 9 31 78 5 2 5 Array of lists: [[15, 5, 2, 6, 7, 888, 9, 3], [5, 4, 7, 9, 31, 78, 5, 2, 5]]
Summary
- Define a Function:
get_list_from_inputto convert user input into a list of integers. - Get User Input: Use the function twice to get two lists.
- Combine Lists: Create a list of these two lists.
- Print Result: Display the combined list of lists.
This sequence of steps results in a nested list where each sub-list contains integers provided by the user.
You may also learn,