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

I have isolated the problem in the second block of code below (if you
I have to print list of objects to a text file with table format.
we are trying to create a calendar function in python. we have created a
I am using the code from this tutorial to parse a CSV file and
I am creating an application in .NET that will serve as a second UI
I need a callback function that is almost exactly the same for a series
I've upgraded a Grails 1.0.4 application to 1.1.1. After upgrading, I'm repeatedly getting Exceptions
I am trying to pass a variable from a function to a class. Example
I'm trying to write a function that checks if a character is within a
I am trying to get a user to enter a number between 1 and

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.