[Python beginner] Divide one list (5 lines).

Conclusion

Let's remember this


split_list(l, n)

#l:list
#n:Number of elements in the sublist

How to use

I'll break the list


def split_list(l, n):
    for idx in range(0, len(l), n):
        yield l[idx:idx + n]
 
l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] #Original list
result = list(split_list(l, 3)) #How many elements to divide

print(result) # [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]

Recommended Posts

[Python beginner] Divide one list (5 lines).
[Python] list
Beginner ABC154 (Python)
Beginner ABC156 (Python)
Python basics: list
python beginner memo (9.2-10)
python beginner memo (9.1)
Python beginner notes
[Beginner] Python array
Beginner ABC155 (Python)
Python> Comprehension / Comprehension> List comprehension
[Beginner] Python functions
Beginner ABC157 (Python)
PyQ ~ Python Beginner ~
Python beginner memo (2)
Python list manipulation
Python beginner Zundokokiyoshi
Sorted list in Python
Python Exercise 2 --List Comprehension
List of python modules
Python> list> extend () or + =
Python text reading for multiple lines and one line
python Zundko One Liner
Python list comprehension speed
Filter List in Python
python unittest assertXXX list
[Python beginner] Extract prefectures and cities from addresses (3 lines).
Python3 List / dictionary memo
[Memo] Python3 list sort
OpenCV3 Python API list
Python error list (Japanese)
List find in Python
Python exception class list
Initialize list with python
Decrypt one line of code in Python lambda, map, list
Python hand play (two-dimensional list)
[Python 3] Prime factorization in 14 lines
Python list, for statement, dictionary
Summary of Python3 list operations
Fizzbuzz in Python (in one line)
curl -I python one liner
python super beginner tries scraping
[Python] Convert list to Pandas [Pandas]
[Python] How to use list 1
Create ToDo List [Python Django]
Python beginner launches Discord Bot
DMD in Python one dimension
Specify multiple list indexes (Python)
Python list is not a list
[Python3] Dijkstra's algorithm with 14 lines
Make python segfault in 2 lines
[Python] Copy of multidimensional list
[Introduction to Python] <list> [edit: 2020/02/22]
Python list and tuples and commas
Paiza Python Primer 4: List Basics
Python list comprehensions and generators
[Python / PyQ] 4. list, for statement
Python #list for super beginners
Web scraping beginner with python
Python install in 2 lines @Windows
Getting list elements in Python