Python Forum
reverse list, incl. nested list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
reverse list, incl. nested list
#3
For general purpose solution recursion seems suitable. However, in case of one level deep and only lists then oneliner will do ("for every element in reverse ordered list give me reverse ordered element if element is list otherwise element itself"):

>>> lst = ["WHAT","IS","THIS",["a nested list", 3, 8, 4.00]]
>>> [list(reversed(el)) if isinstance(el, list) else el for el in reversed(lst)]
[[4.0, 8, 3, 'a nested list'], 'THIS', 'IS', 'WHAT']
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Messages In This Thread
reverse list, incl. nested list - by Livne_ye - Apr-29-2019, 03:08 PM
RE: reverse list, incl. nested list - by buran - Apr-29-2019, 03:53 PM
RE: reverse list, incl. nested list - by perfringo - Apr-29-2019, 04:00 PM
RE: reverse list, incl. nested list - by Livne_ye - May-04-2019, 12:34 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Strange behavior list of list mmhmjanssen 2 209 May-01-2024, 07:16 AM
Last Post: Gribouillis
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,233 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,591 May-01-2023, 09:06 PM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 945 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Сheck if an element from a list is in another list that contains a namedtuple elnk 8 1,885 Oct-26-2022, 04:03 PM
Last Post: deanhystad
Question Keyword to build list from list of objects? pfdjhfuys 3 1,606 Aug-06-2022, 11:39 PM
Last Post: Pedroski55
  Split a number to list and list sum must be number sunny9495 5 2,341 Apr-28-2022, 09:32 AM
Last Post: Dexty
  Updating nested dict list keys tbaror 2 1,302 Feb-09-2022, 09:37 AM
Last Post: tbaror
  Python Program to Find the Total Sum of a Nested List vlearner 8 5,043 Jan-23-2022, 07:20 PM
Last Post: menator01
  How to check if a list is in another list finndude 4 1,868 Jan-17-2022, 05:04 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020