How to get elements from a list, by groups of 10? (or 5, or 3, etc)

beachwood23

What is the Pythonic way of getting elements from the list in groups? I.e., I want the first 10 elements of a list, then the next 10 elements, then the next 10 elements, etc.

thefourtheye

You can use a generator function like this

def getElements(data, n):
    for i in xrange(0, len(data), n):
        yield data[i:i+n]

And then iterate over the generator like this

data = range(100)
for i in getElements(data, 20):
    print i

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
[20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39]
[40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59]
[60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79]
[80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to get elements from list in bash?

From Dev

How to get elements from list in structure by pointer

From Dev

How to display a list in horizontal groups of li elements?

From Dev

how to average groups of elements in a list in R?

From Dev

Take groups of 5 strings from List

From Dev

How to get Distinct elements from a List of List in c#

From Dev

Get all the Quickblox groups list from server

From Dev

Get elements from list in python

From Dev

How do you get elements from list of tuples in SML?

From Dev

How to get only a particular type of elements from a list in Haskell?

From Dev

how to get a specific output from different elements of a list?

From Dev

How to get the elements from a generic List<T> in C#?

From Dev

Find how many groups of 3 are in list

From Dev

Python 3: How to add elements from list as values of a dictionary?

From Dev

How to choose all 2 elements sub list from the list of 3 elements?

From Dev

How to choose all 2 elements sub list from the list of 3 elements?

From Dev

How to get the last 5-10 message from a database?

From Dev

How to extract data from a list with multiple groups?

From Dev

If INT equals 3,5,8,10,13,15 (etc)

From Dev

python get last 5 elements in list of lists

From Dev

How to get the position of elements in a list?

From Dev

How to find the minimum number of groups needed for grouping a list of elements?

From Dev

How to get all value from List inside List in Vue 3

From Dev

How to get all value from List inside List in Vue 3

From Dev

How do I get a list of users not present in a CSV file from /etc/passwd?

From Dev

How do I get the sum of all elements >10 in a given list using chez scheme?

From Dev

How i can iterate list to get 10 elements each time in java

From Dev

How to remove elements from a list?

From Dev

How do I ceil number (etc: 5 to 10)

Related Related

  1. 1

    how to get elements from list in bash?

  2. 2

    How to get elements from list in structure by pointer

  3. 3

    How to display a list in horizontal groups of li elements?

  4. 4

    how to average groups of elements in a list in R?

  5. 5

    Take groups of 5 strings from List

  6. 6

    How to get Distinct elements from a List of List in c#

  7. 7

    Get all the Quickblox groups list from server

  8. 8

    Get elements from list in python

  9. 9

    How do you get elements from list of tuples in SML?

  10. 10

    How to get only a particular type of elements from a list in Haskell?

  11. 11

    how to get a specific output from different elements of a list?

  12. 12

    How to get the elements from a generic List<T> in C#?

  13. 13

    Find how many groups of 3 are in list

  14. 14

    Python 3: How to add elements from list as values of a dictionary?

  15. 15

    How to choose all 2 elements sub list from the list of 3 elements?

  16. 16

    How to choose all 2 elements sub list from the list of 3 elements?

  17. 17

    How to get the last 5-10 message from a database?

  18. 18

    How to extract data from a list with multiple groups?

  19. 19

    If INT equals 3,5,8,10,13,15 (etc)

  20. 20

    python get last 5 elements in list of lists

  21. 21

    How to get the position of elements in a list?

  22. 22

    How to find the minimum number of groups needed for grouping a list of elements?

  23. 23

    How to get all value from List inside List in Vue 3

  24. 24

    How to get all value from List inside List in Vue 3

  25. 25

    How do I get a list of users not present in a CSV file from /etc/passwd?

  26. 26

    How do I get the sum of all elements >10 in a given list using chez scheme?

  27. 27

    How i can iterate list to get 10 elements each time in java

  28. 28

    How to remove elements from a list?

  29. 29

    How do I ceil number (etc: 5 to 10)

HotTag

Archive