Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python split list to list of sublist size n

my_list = ['geeks', 'for', 'geeks', 'like',
           'geeky','nerdy', 'geek', 'love',
               'questions','words', 'life']
 
# Yield successive n-sized
# chunks from l.
def divide_chunks(l, n):
     
    # looping till length l
    for i in range(0, len(l), n):
        yield l[i:i + n]
 
# How many elements each
# list should have
n = 5
 
x = list(divide_chunks(my_list, n))
print (x)
Comment

python split list into n sublists

[input[i:i+n] for i in range(0, len(input), n)]        # Use xrange in py2k
Comment

PREVIOUS NEXT
Code Example
Python :: python read entire file 
Python :: how to sort a dictionary py 
Python :: how to make a separate list of values from dictionaries in python 
Python :: how to install python 3.6.0 on debian 
Python :: tensorflow_version 
Python :: union dataframe pyspark 
Python :: remove punctuation python string library 
Python :: python 2 deprecated 
Python :: found features with object datatype 
Python :: get mode dataframe 
Python :: How to scale a pandas dataframe 
Python :: Change my python working directory 
Python :: program count the number of occurrences of a letter in a string python 
Python :: pandas remove outliers for multiple columns 
Python :: split column by comma pandas 
Python :: python operators 
Python :: Sorting Dataframes by Column Python Pandas 
Python :: change django time zone 
Python :: generate a random letter using python 
Python :: pandas divide one column by another 
Python :: __file__ python 
Python :: sum of 2 numbers in python 
Python :: check integer number python 
Python :: django never_cache example 
Python :: drop duplicate index pandas 
Python :: thread with args python 
Python :: skip to next iteration in for loop python 
Python :: when button is clicked tkinter python 
Python :: flask port 
Python :: check all values in dictionary python 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =