Saturday, November 16, 2024
Google search engine
HomeLanguagesPython Program to Create Two Lists with First Half and Second Half...

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

Python program to create list in first half of the elements with the second half of the elements; In this tutorial, you will learn how to create list in first half of the elements with the second half of the elements in python.

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

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

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

use the following steps to write a python program to create list in first half of the elements with the second half of the elements:

  • 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:

.banner-1-multi-360{border:none!important;display:block!important;float:none!important;line-height:0;margin-bottom:15px!important;margin-left:auto!important;margin-right:auto!important;margin-top:15px!important;max-width:100%!important;min-height:250px;min-width:250px;padding:0;text-align:center!important;width:100%}

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

use the following steps to write a python program to create list in first half of the elements with the second half of the elements 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 Programs

RELATED ARTICLES

Most Popular

Recent Comments