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

  • Home
  • SEARCH
  • 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 9246345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:28:00+00:00 2026-06-18T09:28:00+00:00

My professor posted the function below. I do not fully understand how it’s working.

  • 0

My professor posted the function below. I do not fully understand how it’s working. Could someone explain it?

def rev(a):
    if a == []:
        return []
    else:
        return rev(a[1:]) + [a[0]]
  • 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-06-18T09:28:01+00:00Added an answer on June 18, 2026 at 9:28 am

    What this does is recursively reverses a list. The easiest way to see how it works is to follow through the execution.

    The function takes the string and solves it by returning the reversed version of all but the first item (a[1:]) with the first item appended to the end.

    Note that this is a bad way to do this in a real situation (I am presuming your professor is just showing the idea of recursion), as Python isn’t optimized for recursion. Instead, use the reversed() builtin.

    Also, it isn’t particularly Pythonic code. If one had to have a recursive solution, instead of using the efficient, effective, well-tested, easy-to-use built-in, consider:

    def rev(seq):
        return rev(seq[1:]) + [seq[0]] if seq else []
    
    • We use the ternary operator to condense the if/else.
    • Replacing a with seq makes the function clearer – Python doesn’t have strict typing, so using names that give a clue to what the function takes (in this case, a sequence), makes it clearer.
    • We also replace the a == [] by simply checking seq. As lists evaluate to False when empty, there is no need to compare to an empty list.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I'm working on rewriting a program for a professor, and I have some
My professor posted some review questions for the final exam. And I can't seem
As far as I understand the Metro part of the runtime is not x86
I've already posted this question here , but since it's maybe not that Qt-specific,
I am trying to learn some simple matlab code posted by my professor, and
My professor showed us this code: timerX(int x){ int times(int y){ return x *
My professor posted this online for a lab this week, but I missed the
I can't say I fully understand the script, because classes go beyond me as
My professor provided me with a file called CursorList.cpp that implements a Cursor Linked
I'm presenting a problem my professor showed in class, with my O(n*log(n)) solution: Given

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.