Home > Software design >  How to append list item to the return function
How to append list item to the return function

Time:09-08

     def tab():
        .......
     return[a,b,c,d]
     [a,b,c,d] = tab()
 

How can I append a list to the defined function. I want to attach Z to the a, b, c and d.
This is the function that I've defined but not sure how to append to the returned list.

    reg = ['Z'] 
    df_dict = {} 
    for item in reg:
         df_dict[item].append(a)

CodePudding user response:

If you want to change the value in the function, and not just get the value returned by the function and add to it, your approach should be this:

val = [a,b,c,d]

def tab():
    global val 
    return val

reg = ['Z'] 
    for item in reg:
        val.append(item)

CodePudding user response:

Your tab() function return a list and you want to append something to it.

Therefore:

def tab():
    return [1,2,3,4]

(list_ := tab()).append('Z')

print(list_)

Output:

[1, 2, 3, 4, 'Z']
  • Related