1

I have a loop which generates a value_list each time it runs, at the end of each iteration i want to append all the lists into a one multi dimensional array

I have:

  • value_list = [1,2,3,4] in 1st iteration
  • value_list = [5,6,7,8] in 2nd iteration
  • value list = [9,10,11,12] in 3rd iteration
  • etc...

At the end of each iteration I want one multi dimensional array like

  • value_list_copy = [[1,2,3,4]] in the 1st iteration
  • value_list_copy = [[1,2,3,4],[5,6,7,8]] in the 2nd iteration
  • value_list_copy = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
  • etc...

How could I achieve this?

Thanks

4
  • I rolled back your edit because after that edit the question altogether became a new question. If you have to respect the time the users put in to answer the question you posted. All of a sudden you can't just edit and ask a new question. You can always ask another question. To ask new question click here Commented Apr 9, 2020 at 9:45
  • sorry...i am new to this... will post a different question Commented Apr 9, 2020 at 9:52
  • Don't be... BTW Welcome to Stack Overflow. I just wanted to tell how things work here. ;) Commented Apr 9, 2020 at 9:55
  • @PriyaRamakrishnan You can mark any of the answers as your solution to thank the people taking time answering your question (and it will also gives you some additional reputation). Cheers and Welcome to the best community in the world~! Commented Apr 10, 2020 at 4:10

6 Answers 6

3

You can use a nested comprehension and itertools.count:

from itertools import count, islice

cols = 4
rows = 5

c = count(1)
matrix = [[next(c) for _ in range(cols)] for _ in range(rows)]
# [[1, 2, 3, 4], 
#  [5, 6, 7, 8], 
#  [9, 10, 11, 12], 
#  [13, 14, 15, 16], 
#  [17, 18, 19, 20]]

The cool kids might also want to zip the count iterator with itself:

list(islice(zip(*[c]*cols), rows))
# [(1, 2, 3, 4), 
#  (5, 6, 7, 8), 
#  (9, 10, 11, 12), 
#  (13, 14, 15, 16), 
#  (17, 18, 19, 20)]
Sign up to request clarification or add additional context in comments.

Comments

1

If you are using Python3.8 then use Walrus assignment(:=).

For Syntax and semantic.

count=0
rows=5
cols=4
[[(count:=count+1) for _ in range(cols)] for _ in range(rows)]

Output:

[[1, 2, 3, 4],
 [5, 6, 7, 8],
 [9, 10, 11, 12],
 [13, 14, 15, 16],
 [17, 18, 19, 20]]

Without using :=.

rows=5
cols=4
[list(range(i,i+cols)) for i in range(1,rows*cols,cols)]

1 Comment

@Jean-FrancoisT. IMO the most standout new feature in python3.8
1

Try this:

limit = 10
length_of_elements_in_each_list = 4
[range(i, i+length_of_elements_in_each_list) for i in range(1, limit)]

You can set a limit and length_of_elements_in_each_list according to your need.

2 Comments

This creates a list of range objects not a list of list objects. Even if you do [list(range(i, i+length_of_elements_in_each_list)) for i in range(1, limit)] to make it a list of list objects, the nested lists don't match the pattern described in the question.
@Phillyclause89 I forgot to add [/*]. We can get required result by changing the last line to: print([[*range(i, i+length_of_elements_in_each_list)] for i in range(1, limit)])
0

Try this below :

value_list_copy = []

for i in range(n): # ----------> Assuming n is the number of times your loop is running
    value_list_copy.append(value_list)  # ------ Append your value list in value_list_copy in every iteration

Here you will get an array of arrays.

print(value_list_copy)

Comments

0

Here are two other possible solutions:

Double for loop approach

rows, cols, start = 3, 4, 1

value_list_copy = []
for j in range(rows):
    value_list = []
    for i in range(start, cols + start):
        value_list.append((j*cols)+i)
    value_list_copy.append(value_list)
    print(
        f'value_list = {value_list}\n'
        f'value_list_copy = {value_list_copy}\n'
    )

List comp method

rows, cols, start = 3, 4, 1

value_list_copy_2 = [
    [
        (j*cols)+i for i in range(start, cols + start)
    ] for j in range(rows)
]
print(f'value_list_copy_2 = {value_list_copy_2}')

Python Tutor Link to example code

Comments

0
(lambda x,y: np.arange(1,1+x*y).reshape((x,y)).tolist())(3,4)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.