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 8029239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:22:39+00:00 2026-06-05T00:22:39+00:00

I have a nested list, and I need to reverse every element in the

  • 0

I have a nested list, and I need to reverse every element in the list. But I dont know whether the list is a list of list of list or not.
So example is:

p = [1, [2, 3, [4, [5, 6]]]]
print deep_reverse(p)
#>>> [[[[6, 5], 4], 3, 2], 1]

q =  [1, [2,3], 4, [5,6]]
print deep_reverse(q)
#>>> [ [6,5], 4, [3, 2], 1]

What I have so far is:

def is_list(p):
    return isinstance(p, list)

def deep_reverse(a):
    a.reverse()
    for i in a:
        if is_list(i):
            i.reverse()
            print a

It works well for the second test, the q one, but doest work for the first test.
I am not sure do I need use a recursion to loop the whole thing? How can I modify my code? Thanks.

  • 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-05T00:22:42+00:00Added an answer on June 5, 2026 at 12:22 am

    The reason your code doesn’t work is because if i is a list of lists, you don’t deep_reverse the lists within i.

    You only need to change one line of your code to the following:

    def is_list(p):
        return isinstance(p, list)
    
    def deep_reverse(a):
        a.reverse()
        for i in a:
            if is_list(i):
                deep_reverse(i)  # <=== This is what changed
                print a
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables: Campaigns, Campaign_statistics. I need to output campaigns list with nested
Little help need with jquery sortable. I have a nested list like so: <div
We have many SVN Repo's but many of them have nested projects. I need
I have a nested list like below (but it has 1,000's of the holder
I have lots of entities with nested List<> in each. For example, I have
I have nested list of the following form: my_list = [['Some1', '2', '3.6', '4.5',
I have a nested list like this: <ul class=list> <li class=list_item_type_1> <ul class=list> <li
I have a nested list comprising ~30,000 sub-lists, each with three entries, e.g., nested_list
I have a nested list something like this: PLACES = ( ('CA', 'Canada', (
I have Python nested list that I'm trying to organize and eventually count number

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.