-2

I try to pass lists in a function as args but the results give me a None in the end. Can you explain to me why?

list_1 = [1, 2, 4]
list_2 = [3, 4, 2]


def total_list(*args):
    for item in args:
        for i in item:
            print(i)


print(total_list(list_1, list_2))

Result:

1
2
4
3
4
2
None
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
ReiLuna
  • 1
  • 2

2 Answers2

2
print(total_list(list_1, list_2))

You're also printing the return-value of the total_list function. Since you don't have any return statement, it evaluates to None. That's where the None is coming from.

rdas
  • 20,604
  • 6
  • 33
  • 46
1

Since there is no return value of total_list function, which means the return value is None, when you call the total_list function inside print, it ends up printing None

Call the function outside print, and you will see what's happening

def total_list(*args):
    for item in args:
        for i in item:
            print(i)

list_1 = [1, 2, 4]
list_2 = [3, 4, 2]
total_list(list_1, list_2)

The output will be

1
2
4
3
4
2
Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40