Python Program to Create Two Lists with First Half and Second Half Elements

Python split list into two equal parts with first half and second half; Through this tutorial, you will learn how to Create two lists with first half and second half elements of a list.

Create two lists with first half and second half elements of a list

  1. Write a python program to create two lists with first half and second half elements of a given list.
  2. Python program to create two lists with first half and second half elements of a given list using range slicing.

1: Write a python program to create two lists with first half and second half elements of a given list

  • Define a list.
  • Take input how many element in list from user.
  • Iterate for loop and use input() function to allow user to input element.
  • Append elements in list by using append() method.
  • Divide list length number by 2 and store in variable.
  • To get first half elements using list[:num] and store list1.
  • To get second half elements using list[num:] and store list2.
  • Print list1 and list2.
# write a python program to create two lists with first half and second half 
#elements of a given list.
NumList = []
Number = int(input("How many elements in list :- "))
# condition to check given number is even or odd
if( Number%2 != 0 ):
    print("This program will not accept odd number.")
    exit()
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element :- " %i))
    NumList.append(value)
#number half 
num = int(Number/2)
# Create list1 with half elements (first 3 elements)
list1 = NumList[:num]
# Create list2 with next half elements (next 3 elements)
list2 = NumList[num:]
# print list (s)
print("list : ",NumList)
print("list1: ",list1)
print("list2: ",list2)

After executing the program, the output will be:

How many elements in list :-  6
Please enter the Value of 1 Element :-  1
Please enter the Value of 2 Element :-  2
Please enter the Value of 3 Element :-  3
Please enter the Value of 4 Element :-  4
Please enter the Value of 5 Element :-  5
Please enter the Value of 6 Element :-  6
list :  [1, 2, 3, 4, 5, 6]
list1:  [1, 2, 3]
list2:  [4, 5, 6]

2: Python program to create two lists with first half and second half elements of a given list using range slicing

# write a python program to create two lists with first half and second half 
#elements of a given list.
NumList = []
Number = int(input("How many elements in list :- "))
# condition to check given number is even or odd
if( Number%2 != 0 ):
    print("This program will not accept odd number.")
    exit()
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element :- " %i))
    NumList.append(value)
# divide by 2 the length of list 
n = int(Number/2)
    
# Create list1 with half elements (first 3 elements)
list1 = NumList [0:n]
# Create list2 with next half elements (next 3 elements)
list2 = NumList [n:Number]
# print list (s)
print("list : ",NumList)
print("list1: ",list1)
print("list2: ",list2)

After executing the program, the output will be:

How many elements in list :-  6

Please enter the Value of 1 Element :-  9
Please enter the Value of 2 Element :-  8
Please enter the Value of 3 Element :-  7
Please enter the Value of 4 Element :-  6
Please enter the Value of 5 Element :-  5
Please enter the Value of 6 Element :-  4

list :  [9, 8, 7, 6, 5, 4]
list1:  [9, 8, 7]
list2:  [6, 5, 4]

Recommended Python Tutorials

Leave a Comment