Home  >  Article  >  Backend Development  >  Python program to split a list in half

Python program to split a list in half

WBOY
WBOYforward
2023-09-19 15:13:021122browse

Python program to split a list in half

In Python, a single variable can contain multiple items by using a list. One of the four built-in data types in Python for storing collections of data is a list; the other three are tuples, sets, and dictionaries, each with its own purpose.

What is a list?

Square brackets are used to build lists. The most powerful tools in Python are lists, since they are not necessarily homogeneous. Data types like integers, strings, and objects can all be found in a list. Since lists are mutable, they can be changed even after they are created.

In this article, we will explore various ways to split a list in half using Python programming. List is one of the mutable data types that can store collections of objects. With these tips, you'll be able to easily split any list in half!

Use slicing technology

In the first scenario, the list is split into two halves or halves. Depending on the length of the list, these halves can be equally or unevenly sized. Lists can be split using slicing methods.

algorithm

  • Create a list and initialize its middle index with half its length.

  • Split it in half, indexing from the start to the middle and indexing from the middle to the end.

  • Print out the original list and the half list.

  • Sort each half and then merge them into a sorted list.

  • Finally, print out this new merged and sorted list.

Example

The following example creates a list of 6 elements and then sets the index to 3. The list is then split into two halves based on that index - the first half is all the elements before the index, and the second half is all the elements after it. Finally, it prints out both halves of the list.

#create list
list_1 = [10,20,30,40,50,60]
index = 3
first_half = list_1 [:index]
second_half = list_1 [index:]
print('The primary list is: ',list_1)
print("First half of list is ",first_half)
print("Second half of list is ",second_half)

Output

The primary list is: [10, 20, 30, 40, 50, 60]
First half of list is [10, 20, 30]
Second half of list is [40, 50, 60]

Here, in the method explained above, we predefined the index and length of the list. What if the split index or the size of the two parts is not specified? The next step is to determine the middle index of the list, which can be done by multiplying the length of the list by 2. However, if the length of the list is an odd integer or the list is not symmetrical, then when we divide by the length of the list, we will get a floating point value. list. To round the result, we will use the round down operator (//).

Example

In this method, our main concern is to solve a different condition, that is, if the number of elements requested by the user is an odd number, what is the process of completing the task. Here, the split function returns two lists that are not equal because the list has an odd number of elements. The middle is (5/2) = 2.5 because the list is 5 items long. The nearest integer value less than or equal to the division result is returned by the rounding operator. In this case, the round down operator produces 2, not 2.5.

algorithm

  • Define a function that accepts a list of numbers and asks the user to enter a value.

  • Use a for loop to traverse the list,

  • Then use the append() function to divide each number by 2 and find its middle index.

  • Prompts the user for input after completion.

The following example shows a program splitting a list of numbers entered by the user into two halves. It asks the user to enter the number of elements they want to add to the list, then prompts them to enter one element at a time.

The middle index is calculated by dividing the length of the list by 2 and then using that index to call split_list() , which uses slicing to separate the first half of the list from the second half part and returns two lists respectively.

def split_list(input_L,n):
   first_half = input_L[:n]
   second_half = input_L[n:]
return first_half,second_half
if __name__ == "__main__" :
   list_1 = []
   length = int(input("Enter the number of elements you want in list : "))
   for i in range(0, length):
      item = int(input("Enter the element for list "+str(i+1)+" :"))
      list_1.append (item)

   middle_index = length//2
   first,second = split_list (list_1,middle_index)
   print ("Primary list: ", list_1)
   print ("First half of the list is: ", first)
   print ("second half of the list is: ", second)

Output

When executing the above program, the following output will be generated -

Enter the number of elements you want in list: 5
Enter the element for list 1:98
Enter the element for list 2:60
Enter the element for list 3:45
Enter the element for list 4:33
Enter the element for list 5:55
Primary list: [98, 60, 45, 33, 55]
First half of the list is: [98, 60]
second half of the list is: [45, 33, 55]

in conclusion

In this article, we use python to split a list in half using different methods.

The above is the detailed content of Python program to split a list in half. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete