So I have a list that contins about 1400 elements. What I want to do is dividing elements into groups each containing 7 elements in order. How can I do it.
Asked
Active
Viewed 32 times
1 Answers
4
You can do something like this:
lst = [1,2,3,4,5,6,7,8,9,10,11,12,13,14]
[lst[i:i + 7] for i in range(0, len(lst), 7)]
#output
[[1, 2, 3, 4, 5, 6, 7], [8, 9, 10, 11, 12, 13, 14]]

Talha Tayyab
- 8,111
- 25
- 27
- 44
-
Please look for duplicates before answering. Questions like this have usually been asked and answered on SO many times before. – Pranav Hosangadi Jul 03 '23 at 16:51
-
think you want `sorted(lst[i:i + 7])` based on the description but not 100%. – Carbon Jul 03 '23 at 16:51
-
@PranavHosangadi if a question closes as duplicate, can you get points for that 'answer'? – Carbon Jul 03 '23 at 16:52
-
1@Carbon you don't get points for voting to close. You keep points from any upvotes or downvotes to an actual answer, but those get taken away if the question is deleted – Pranav Hosangadi Jul 03 '23 at 16:55