-1

I want to create multiple list by using a single loop with some condition. I know how to create one list which is done by appending, but here all the results of loop goes in one single list which is not what I want. So lets say we run a loop on first 100 numbers and I want to create multiple list where first list contains numbers till 5 , second from 6 to 10, third from 11 to 15 and so. This code is just for one list created by me.

number = range(100)
first = []
for i in number:
    first.append(i)
first
Mehrdad Pedramfar
  • 10,941
  • 4
  • 38
  • 59

6 Answers6

1

Something like that:

l = []
for x in range(6, 102, 5):
  l.append([y for y in range(x-5, x)])

Output:

[[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, 100]]

The range function takes three parameters start, stop and step.

adnanmuttaleb
  • 3,388
  • 1
  • 29
  • 46
1

Try this, This will create what you want:

lists = [] 
for i in range(1, 100, 5): # range(start, end, size_of_each_list)
    lists.append(list(range(i,i + 5)))

The lists will be:

[[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, 100]]
Mehrdad Pedramfar
  • 10,941
  • 4
  • 38
  • 59
0

I would do a list comprehension similar to this:

[[i, i+1, i+2, i+3, i+4] for i in range (1, 100, 5)]

The output would look like this:

[[1, 2, 3, 4, 5], 
...,
[96, 97, 98, 99, 100]]

https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions

MasOOd.KamYab
  • 944
  • 11
  • 25
0

This is one way to do it. Basically you create an iterator from your list and then get all the lists you want from it.

number = range(100)
number_iter = iter(number)
lists = []

while True:
  try:
    lists.append([next(number_iter) for _ in range(5)])
  except StopIteration as e:
    break
lists

this has the advantage that your initial `number' list can be anything...

fixmycode
  • 8,220
  • 2
  • 28
  • 43
0

I would use second list inside the first.

number = range(100)
cnt = 0
first = []
second = []

for i in number:
    cnt += 1
    second.append(i)

    if cnt == 5:
        first.append(second)
        cnt = 0
        second = []

first

output:

[[0, 1, 2, 3, 4],
 [5, 6, 7, 8, 9],
...
 [95, 96, 97, 98, 99]]
shimo
  • 2,156
  • 4
  • 17
  • 21
0

It is possible. We can do this by directly creating variables in our globals() environment.
(Use locals() if it should exist only in the enclosing function).


You can do this with the following code:

# Run the loop
for i in range(100):

    # for 0-4, list_name = 'list_1'
    list_name = 'list_' + str(i//5 + 1)
    # Try to append to that list
    try:
        globals()[list_name].append(i)
    # If if doesn't exist, create it on the run!
    except KeyError:
        globals()[list_name] = [i]

This globals()[list_name] = [i] first:

  • Gets the module environment dictionary.
  • Creates list_name variable.
  • Initialises it to a list containing i.

Let us print them all:

for i in range(20):
    # Print list_1 through list_20
    list_name = 'list_' + str(i+1)
    print(list_name + ':', globals()[list_name])

You get:

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

Note: Why not have some number fun! See python inflect package.

Jaideep Shekhar
  • 808
  • 2
  • 7
  • 21