2
list_of_numbers = [10.0, 12.0, 14.0, 16.0, 15.0, 13.0, 10.0, 9.2, 11.7, 14.8, 16.5, 14.8, 13.8, 10.2, 9.5, 13.0, 14.4, 17.2, 15.4, 12.5, 12.1, 10.0, 12.4, 11.9, 16.8, 15.6, 14.6, 10.4, 10.4, 11.0, 12.2, 18.8, 13.9, 12.0, 6.8, 11.2, 9.4, 12.6, 15.5, 14.0, 11.2, 12.3, 14.3, 11.7, 13.9, 13.4, 21.4, 13.7, 12.6]

Out of this list i want to create a list of lists with 7 elements each. How do i do? (There are 49 elements in the list so i want to i want to create a list of 7 lists in it. Order should remain the same as in the list_of_numbers

2 Answers2

2

You can use a simple combination of slicing and list comprehension:

result = [list_of_numbers[i:i+7]
          for i in range(0, len(list_of_numbers), 7)]
6502
  • 112,025
  • 15
  • 165
  • 265
1

You can use zip with iter as follows:

zip(*[iter(list_of_numbers)]*7)

Output:

[(10.0, 12.0, 14.0, 16.0, 15.0, 13.0, 10.0), (9.2, 11.7, 14.8, 16.5, 14.8, 13.8, 10.2), (9.5, 13.0, 14.4, 17.2, 15.4, 12.5, 12.1), (10.0, 12.4, 11.9, 16.8, 15.6, 14.6, 10.4), (10.4, 11.0, 12.2, 18.8, 13.9, 12.0, 6.8), (11.2, 9.4, 12.6, 15.5, 14.0, 11.2, 12.3), (14.3, 11.7, 13.9, 13.4, 21.4, 13.7, 12.6)]
venpa
  • 4,268
  • 21
  • 23