Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3596260
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T19:57:53+00:00 2026-05-18T19:57:53+00:00

for the code below: def printList(L): if L: print L[0] printList(L[1:]) I can have

  • 0

for the code below:

def printList(L):

   if L:

       print L[0]

       printList(L[1:])

I can have sequence diagram like this:

# NON PYTHON PSEUDO CODE

PrintList([1,2,3])

  prints [1,2,3][0] => 1

  runs printList([1,2,3][1:]) => printList([2,3])

  => we're now in printList([2,3])

        prints [2,3][0] => 2

        runs printList([2,3][1:]) => printList([3])

    => we are now in printList([3])

          prints [3][0] => 3

          runs printList([3][1:]) => printList([])

          => we are now in printList([])

                "if L" is false for an empty list, so we return None

    => we are back in printList([3])

          it reaches the end of the function and returns None

  => we are back in printList([2,3])

    it reaches the end of the function and returns None

=> we are back in printList([1,2,3])

  it reaches the end of the function and returns None

So my question is if I change the code to:

def printList(L):

   if L:
       print L[0]
       printList(L[1:])
       print L[0]

How would the sequence diagram change, I want to understand what exactly happens during the execution of this code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T19:57:54+00:00Added an answer on May 18, 2026 at 7:57 pm

    The print statement called after the recursive calls will all get hit “on the way back up”. That is, each of your statements: “it reaches the end of the function and returns None” can be changed to “it prints the current value of L[0], reaches the end of the function, and returns None”, which will be 3, 2, and 1 respectively.

    Like so:

    PrintList([1,2,3])
    prints [1,2,3][0] => 1
    runs printList([1,2,3][1:]) => printList([2,3])
    => we're now in printList([2,3])
        prints [2,3][0] => 2
        runs printList([2,3][1:]) => printList([3])
        => we are now in printList([3])
            prints [3][0] => 3
            runs printList([3][1:]) => printList([])
            => we are now in printList([])
                "if L" is false for an empty list, so we return None
            => we are back in printList([3])
            prints [3][0] => 3
            it reaches the end of the function and returns None
        => we are back in printList([2,3])
       prints [2,3][0] => 2
       it reaches the end of the function and returns None
    => we are back in printList([1,2,3])
    prints [1,2,3][0] => 1
    it reaches the end of the function and returns None
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

for the code below: def printList(L): if L: print L[0] printList(L[1:]) I can have
Is this a valid function pointer code below, In views , def change(request): dict={}
My code is like below: def getAllVehicles(self): try: vehobj = Vehicles.objects.all() except VehicleDoesNotExists, e:
With the example code below: mat = [] x = 5 def add(c,b): print
See the code below class HelloWorld: def print(self): print Hello World Is HelloWorld.print(self) called
Hi I have a code as given below def value(): file=open('C:/Documents and Settings/242481/My Documents/file.csv','r')
I have the below code: def numbers(): '''returns 2 random numbers between 1 and
I can access a python function's attribute inside of function itself by below code:
Right now I have only one condition in my Projects.paginate Code is below def
The code below returns none . How can I fix it? I'm using Python

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.