Computer Science, asked by sarinsergiev, 3 months ago

Write a python program to exchange the first half elements of the list with the second half elements assuming list having even number of elements.

Answers

Answered by Anonymous
5

Answer:

By considering length of the list

Let suppose list has n elements, then we can use list[0:n/2] and list[n/2:n].

Consider the program:

If there are ODD numbers of elements in the list, program will display message "List has ODD number of elements." And exit.

# define a list

list = [10, 20, 30, 40, 50, 60]

# get the length of the list

n = len(list)

# condition to check length is EVEN or not

# if lenght is ODD, show message and exit

if( n%2 != 0 ):

print "List has ODD number of elements."

exit()

# Create list1 with half elements (first 3 elements)

list1 = list [0:n/2]

# Create list2 with next half elements (next 3 elements)

list2 = list [n/2:n]

# print list (s)

print "list : ",list

print "list1: ",list1

print "list2: ",list2

Output

list : [10, 20, 30, 40, 50, 60]

list1: [10, 20, 30]

list2: [40, 50, 60]

TOP Interview Coding Problems/Challenges

Run-length encoding (find/print frequency of letters in a string)

Sort an array of 0's, 1's and 2's in linear time complexity

Checking Anagrams (check whether two string is anagrams or not)

Relative sorting algorithm

Finding subarray with given sum

Find the level in a binary tree with given sum K

Check whether a Binary Tree is BST (Binary Search Tree) or not

1[0]1 Pattern Count

Capitalize first and last letter of each word in a line

Print vertical sum of a binary tree

Print Boundary Sum of a Binary Tree

Reverse a single linked list

Greedy Strategy to solve major algorithm problems

Job sequencing problem

Root to leaf Path Sum

Exit Point in a Matrix

Find length of loop in a linked list

Toppers of Class

Print All Nodes that don't have Sibling

Transform to Sum Tree

Shortest Source to Destination Path

Answered by Shwetatheprincess
3

In this python tutorial, we would like to share with you the following:

Write a 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: 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.

Hope it's helpful

Have a nice day

Plz follow and thanks my answers

Plz mark me as brainliest...

Similar questions