# Example of an python method for splitting a list (list)

• 2020-05-30 20:34:00
• OfStack

preface

In daily development, some API interfaces will limit the number of requested elements, so it is necessary to divide a large list into a fixed small list, and then conduct relevant processing. This paper has collected a few simple methods, and Shared them for your reference and learning. The following is a detailed introduction:

Methods the sample

``````
#1. Split the large list as 3 A small list of elements, not enough 3 The element of 1 List output
In [17]: lst
Out[17]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [18]: for i in range(0,len(lst),3):
...:   print lst[i:i+3]
...:
[0, 1, 2]
[3, 4, 5]
[6, 7, 8]
[9]

#2. I'm going to make a little bit of an improvement, and I'm going to do a little bit of a list derivation, and I'm going to put it all in 1 A list of the
In [35]: lst
Out[35]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [36]: b = [lst[i:i+3] for i in range(0,len(lst),3)]

In [37]: b
Out[37]: [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]

In [38]:

#3. Not really lambda , 1 If you don't understand, look at the logic or the logic above
In [10]: f = lambda a:map(lambda b:a[b:b+3],range(0,len(a),3))

In [11]: lst
Out[11]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [12]: f(lst)
Out[12]: [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]``````

conclusion

Related articles: