How to append list item to the return function

Question:

     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)
Asked By: Josh

||

Answers:

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']
Answered By: Vlad

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)
Answered By: Aric a.
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.