How do you split a list into half in python?

Last update on August 19 2022 21:50:49 (UTC/GMT +8 hours)

Python List: Exercise - 78 with Solution

Write a Python program to split a given list into two parts where the length of the first part of the list is given.

Sample Solution:

Python Code:

def split_two_parts(n_list, L):
    return n_list[:L], n_list[L:]
n_list = [1,1,2,3,4,4,5, 1]
print("Original list:") 
print(n_list)
first_list_length = 3
print("\nLength of the first part of the list:",first_list_length)
print("\nSplited the said list into two parts:")
print(split_two_parts(n_list, first_list_length))


Sample Output:

Original list:
[1, 1, 2, 3, 4, 4, 5, 1]

Length of the first part of the list: 3

Splited the said list into two parts:
([1, 1, 2], [3, 4, 4, 5, 1])

Flowchart:

How do you split a list into half in python?

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to decode a run-length encoded given list.
Next: Write a Python program to remove the K'th element from a given list, print the new list.

Python: Tips of the Day

Unknown Arguments Using *arguments:

If your function can take in any number of arguments then add a * in front of the parameter name:

def myfunc(*arguments):
 for a in arguments:
   print a
myfunc(a)
myfunc(a,b)
myfunc(a,b,c)

Given a nested 2D list, the task is to split the nested list into two lists such that first list contains first elements of each sublists and second list contains second element of each sublists. Method #1: Using map, zip() 

Python3

ini_list = [[1, 2], [4, 3], [45, 65], [223, 2]]

print ("initial list", str(ini_list))

res1, res2 = map(list, zip(*ini_list))

print("final lists", res1, "\n", res2)

Output:

initial list [[1, 2], [4, 3], [45, 65], [223, 2]]
final lists [1, 4, 45, 223] 
 [2, 3, 65, 2]

  Method #2: Using list comprehension 

Python3

ini_list = [[1, 2], [4, 3], [45, 65], [223, 2]]

print ("initial list", str(ini_list))

res1 = [i[1] for i in ini_list]

res2 = [i[0] for i in ini_list]

print("final lists", str(res1), "\n", str(res2))

Output:

initial list [[1, 2], [4, 3], [45, 65], [223, 2]]
final lists [2, 3, 65, 2] 
 [1, 4, 45, 223]

  Method #3: Using operator.itemgetter() 

Python3

from operator import itemgetter

ini_list = [[1, 2], [4, 3], [45, 65], [223, 2]]

print ("initial list", str(ini_list))

res1 = list(map(itemgetter(0), ini_list))

res2 = list(map(itemgetter(1), ini_list))

print("final lists", str(res1), "\n", str(res2))

Output:

initial list [[1, 2], [4, 3], [45, 65], [223, 2]]
final lists [1, 4, 45, 223] 
 [2, 3, 65, 2]

Method #4 : Using extend() method

Python3

ini_list = [[1, 2], [4, 3], [45, 65], [223, 2]]

print ("initial list", str(ini_list))

x=[]

for i in ini_list:

    x.extend(i)

res1=[]

res2=[]

for i in range(0,len(x)):

    if(i%2==0):

        res1.append(x[i])

    else:

        res2.append(x[i])

print("final lists", str(res1), "\n", str(res2))

Output

initial list [[1, 2], [4, 3], [45, 65], [223, 2]]
final lists [1, 4, 45, 223] 
 [2, 3, 65, 2]


Can I split a list in Python?

To split the elements of a list in Python: Use a list comprehension to iterate over the list. On each iteration, call the split() method to split each string. Return the part of each string you want to keep.

How do you split a list into parts in Python?

To split a list into n parts in Python, use the numpy. array_split() function. The np. split() function splits the array into multiple sub-arrays.

How do you cut a list in Python?

How to slice a list, string, tuple in Python.
Basic usage of slices. [start:stop] [start:stop:step].
Extract from the end with a negative value. Negative values for start and stop. ... .
Slice object by slice().
Assigning values by slices..
Slices for a list of lists..
Slices make shallow copy..
Slices for strings and tuples..

How do I split a list into multiple lists?

Split Lists into Chunks Using a For-Loop.
We instantiate two lists: a_list , which contains the items of our original list, and chunked_list , which is empty..
We also declare a variable, chunk_size , which we've set to three, to indicate that we want to split our list into chunks of size 3..