-1

I was trying to add elements into a list array something like this

a = 1
b = 2
c = 3
d = [[],[],[]]
for i in range(3):
    d[i] = [a,b,c]
print(d)

what i want d to have is [[1,2,3], [1,2,3], [1,2,3]] but dynamically without having to specify the number of [] inside the list

how do this dynamically?

Shashank Setty
  • 138
  • 1
  • 9

3 Answers3

1

You want something like this.?

a = 1
b = 2
c = 3
d = []
for i in range(3):
    d.append([a,b,c])

print(d)

Output:

[[1, 2, 3], [1, 2, 3], [1, 2, 3]]
krishna
  • 1,029
  • 6
  • 12
  • thank you! this was exactly what I was looking for. – Shashank Setty Apr 12 '20 at 12:42
  • 1
    @ShashankSetty how is this answer "dynamic"? – Nick Apr 12 '20 at 12:56
  • @Nick +1 - it seems the original question asked isn't what the OP is really trying to accomplish. – SteveK Apr 12 '20 at 13:00
  • @SteveK, that's correct I was just tryin to demonstrate what I wanted to happen, my code was too complex to add here, Nick it's dynamic because I don't have to set the list size in my post I loop 3 times for an example but the looping time is dynamic in my original code. – Shashank Setty Apr 12 '20 at 13:13
1

Have your input be a list of values and make a function to return your square matrix. This requires no knowledge of how many lists are to be generated ahead of time.

a = 1
b = 2
c = 3

def square_matrix(values : list) -> list:
    d = []

    for _ in range(len(values)):
        d.append([x for x in values])

    return d

sq_mat = square_matrix([a, b, c]) # [[1,2,3], [1,2,3], [1,2,3]]

This makes the assumption that you want as many lists as you have values. If you want the number of lists to be different from the number of values, then change the function to something like this:

def nested_lists(values : list, num_of_lists : int) -> list:
    d = []
    for _ in range(num_of_lists):
        d.append([x for x in values])
    return d

my_lists = nested_lists([a,b,c], 2) # [[a,b,c], [a,b,c,]]
SteveK
  • 995
  • 5
  • 17
  • Where in OPs question do they say they want a square matrix? – Nick Apr 12 '20 at 12:30
  • 1
    see [list-of-lists-changes-reflected-across-sublists-unexpectedly](https://stackoverflow.com/questions/240178/list-of-lists-changes-reflected-across-sublists-unexpectedly) - this is one of a bout a dozend **very common beginner pythonist problem**s closed down as dupes on SO - our code is activly introducing hard to find bugs into the problem. – Patrick Artner Apr 12 '20 at 12:40
  • 1
    and now its about the same as nicks answer - you jusst create the list once,use a list comp to deambiguate it inside your function and assume its a square matrix instead of assuming you need to fill d as often as d had inner empty lists prepared – Patrick Artner Apr 12 '20 at 12:46
  • @Nick I have updated my answer providing an alternative approach to be truly dynamic. – SteveK Apr 12 '20 at 13:05
-2

If you just want to fill the list with predefined values you could do something like this

d = [[a,b,c]]*3
BeneSim
  • 80
  • 5
  • I don't think the number of lists (3 in this case) is known ahead of time. – SteveK Apr 12 '20 at 12:14
  • Well in this case just replace the `3` with whatever defines the length :) – BeneSim Apr 12 '20 at 12:15
  • 2
    this adds the same reference 3 times, if you do `d[0][1] = 42` it changes in all 3 of them because: same reference – Patrick Artner Apr 12 '20 at 12:15
  • 2
    see [list-of-lists-changes-reflected-across-sublists-unexpectedly](https://stackoverflow.com/questions/240178/list-of-lists-changes-reflected-across-sublists-unexpectedly) - this is one of a bout a dozend very common beginner pythonist problems closed down as dupes on SO – Patrick Artner Apr 12 '20 at 12:15