How do you split a list in a list python?

So I want to split a list of lists.

the code is

myList = [['Sam has an apple,5,May 5'],['Amy has a pie,6,Mar 3'],['Yoo has a Football, 5 ,April 3']]

I tried use this:

for i in mylist:
  i.split[","]

But it keeps give me error message

I want to get:

["Amy has a pie" , "6" , "Mar 3"] THis kinds of format

A list can be split based on the size of the chunk defined. Splitting a list into n parts returns a list of n lists containing an equal number of list elements. If the number of lists, n, does not evenly divide into the length of the list being split, then some lists will have one more element than others.

Let’s traditionally split the list.

To split a list in Python, call the len[iterable] method with iterable as a list to find its length and then floor divide the length by 2 using the // operator to find the middle_index of the list.

list = [11, 18, 19, 21]

length = len[list]

middle_index = length // 2

first_half = list[:middle_index]
second_half = list[middle_index:]

print[first_half]
print[second_half]

Output

[11, 18]
[19, 21]

As you can see from the output, we split the list in the exact half. We used the colon operator[:] to access the first and second half of the split list.

How to split a list into n 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.

The numpy array_split[] method returns the list of n Numpy arrays, each containing approximately the same number of elements from the list.

import numpy as np

listA = [11, 18, 19, 21, 29, 46]

splits = np.array_split[listA, 3]

for array in splits:
    print[list[array]]

Output

[11, 18]
[19, 21]
[29, 46]

In this example, we split the list into 3 parts.

Split a List Into Even Chunks of N Elements in Python

A list can be split based on the size of the chunk defined. This means that we can determine the size of the chunk. If the subset of a list doesn’t fit in the size of the defined chunk, fillers need to be inserted in the place of the empty element holders. We will be using None as filters to fill in those empty element holders.

def list_split[listA, n]:
    for x in range[0, len[listA], n]:
        every_chunk = listA[x: n+x]

        if len[every_chunk] < n:
            every_chunk = every_chunk + \
                [None for y in range[n-len[every_chunk]]]
        yield every_chunk


print[list[list_split[[11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 111, 112, 113], 7]]]

Output

[[11, 21, 31, 41, 51, 61, 71], [81, 91, 101, 111, 112, 113, None]]

The list has been split into equal chunks of 7 elements each.

The above list_split[] function takes the arguments: listA for the list and chunk_size for a number to split it by. Then, the function iterates through the list with an increment of the chunk size n.

Each chunk is expected to have the size given as an argument. If there aren’t enough elements to split the same size, the remaining unused elements are filled with None.

That is it for splitting a list in Python.

See also

Split string with multiple parameters

Split line in Python

Python String rsplit[]

Python String splitlines[]

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]


How do you split a list inside a list Python?

The split[] method of the string class is fairly straightforward. It splits the string, given a delimiter, and returns a list consisting of the elements split out from the string. By default, the delimiter is set to a whitespace - so if you omit the delimiter argument, your string will be split on each whitespace.

How do you split a single list into multiple lists?

Split Python Lists into Chunks Using a List Comprehension.
We declare a variable chunk_size to determine how big we want our chunked lists to be..
For our list comprehensions expression, we index our list based on the ith to the i+chunk_sizeth position..

How do you separate a list?

Usually, we use a comma to separate three items or more in a list. However, if one or more of these items contain commas, then you should use a semicolon, instead of a comma, to separate the items and avoid potential confusion.

How do you split a list in half in Python?

Split the list in half. Call len[iterable] with iterable as a list to find its length. Floor divide the length by 2 using the // operator to find the middle_index of the list. Use the slicing syntax list[:middle_index] to get the first half of the list and list[middle_index:] to get the second half of the list.

Chủ Đề