-1

I have a function:

def get_val():
    for a in b:
        c = b['values']
        print (c)

When I call the function get_val(), I get the result which is a long list of values with numbering up to 1000. type(c) is float.

Now I want to store this result from the function into a variable e. I tried using return c instead of print(c)

Return function is not convenient because it will only return a line instead of the whole 1000.

Matthias
  • 12,873
  • 6
  • 42
  • 48

2 Answers2

0

One-liner in Python using list comprehension:

def get_val(b):
    # Do something else
    return [i["values"] for i in b]

Note: I am assuming you mistyped a['values'] and you meant i[values]

abhiarora
  • 9,743
  • 5
  • 32
  • 57
0

I finally was able to find a way around it with some help.

outside the function

create an empty list and run it so it's defined.

     new_list = []

then go back to the function and append c

so you have:

    def get_val():
      for a in b:
          c = b['values']
          new_list.append(c)