-2

I want to initialize b and c to zero and return output as two list of zeros. but it is returning as tuple

def initialize(dim):
    '''In this function, we will initialize bias value 'B' and 'C'.'''
    b = np.zeros(dim)
    c = np.zeros(dim)
    return b.tolist(),c.tolist()
pk_11
  • 41
  • 8
  • Where are you stuck? If you want a list, then return a list. How to return items is covered in any tutorial on functions and return. – Prune Dec 02 '20 at 22:29
  • want b and c to be the two separate list as b = [0,0,0,0] c = [0,0,0,0] – pk_11 Dec 02 '20 at 22:30
  • 2
    Your code should return a tuple of two lists. What are you getting instead? Please supply the expected [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Prune Dec 02 '20 at 22:33
  • 1
    Your main program should read something like `main_b, main_c = initialize(50)` – Prune Dec 02 '20 at 22:34

2 Answers2

1

return b.tolist(),c.tolist() means a tuple of two lists is being returned; like:

([a, b, c], [1, 2, 3])

If you instead concatenated the lists together, they would be returned as one list:

return b.tolist() + c.tolist()

Which returns:

[a, b, c, 1, 2, 3]
Random Davis
  • 6,662
  • 4
  • 14
  • 24
  • want b and c to be the two separate list as b = [0,0,0,0] c = [0,0,0,0] – pk_11 Dec 02 '20 at 22:28
  • Okay so what should the function return, then? You said "list of zeros", which I interpreted as "_a_ list of zeros". Are you saying you actually do want to return two lists, but then have the calling code assign the returned lists to two separate variables? Like `b, c = initialize(dim)`? In that case you could keep your original code. – Random Davis Dec 02 '20 at 22:32
0

When you call this function the returns will be a tuple of the values so you can set the left hand side like so:

b,c = initialize(size)

Then b and c will be set from the return of the function.

Tom Myddeltyn
  • 1,307
  • 1
  • 13
  • 27
  • 1
    If this ends up being the answer, then this question would certainly be a duplicate of [this](https://stackoverflow.com/questions/354883/how-do-i-return-multiple-values-from-a-function). – Random Davis Dec 02 '20 at 22:34