By newman


2010-04-08 10:41:09 8 Comments

def foo(a):
    a.append(1)
    if len(a) > 10:
        print a
        return a
    else:
        foo(a)

Why this recursive function returns None (see transcript below)? I can't quite understand what I am doing wrong.

In [263]: x = []

In [264]: y = foo(x)
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

In [265]: print y
None

1 comments

@Ignacio Vazquez-Abrams 2010-04-08 10:44:34

You don't return anything in the else clause:

    else:
        return foo(a)

@newman 2010-04-08 10:46:43

oh duh, stupid me. Thanks!

@Anwarvic 2017-10-05 11:29:56

Where were you two hours ago !!

Related Questions

Sponsored Content

11 Answered Questions

[SOLVED] Getting the last element of a list

  • 2009-05-30 19:28:53
  • Janusz
  • 1651448 View
  • 1708 Score
  • 11 Answer
  • Tags:   python list indexing

7 Answered Questions

[SOLVED] How do I get the number of elements in a list?

  • 2009-11-11 00:30:54
  • y2k
  • 3082256 View
  • 1783 Score
  • 7 Answer
  • Tags:   python list

39 Answered Questions

[SOLVED] How to make a flat list out of list of lists

25 Answered Questions

[SOLVED] How do I concatenate two lists in Python?

20 Answered Questions

30 Answered Questions

[SOLVED] How do I check if a list is empty?

  • 2008-09-10 06:20:11
  • Ray Vega
  • 2257059 View
  • 3237 Score
  • 30 Answer
  • Tags:   python list

21 Answered Questions

15 Answered Questions

[SOLVED] What are metaclasses in Python?

28 Answered Questions

[SOLVED] Finding the index of an item given a list containing it in Python

  • 2008-10-07 01:39:38
  • Eugene M
  • 3277766 View
  • 2697 Score
  • 28 Answer
  • Tags:   python list indexing

56 Answered Questions

[SOLVED] Calling an external command in Python

Sponsored Content