Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 reverse list, incl. nested list
#1
I am trying to write a code which will generate a nested list and the main list in a reverse order:
1.my_third_list = ["WHAT","IS","THIS",["a nested list", 3, 8, 4.00]]

meaning, once i print the code, the output that i want to see is:
[4.0, 8, 3, 'a nested list'],'THIS', 'IS', 'WHAT']
the command that i wrote for this:
2.print(my_third_list[:[[3][::-1]]:-1])
but instead of the wanted output, i get this type error:
TypeError: slice indices must be integers or None or have an __index__ method

why does it happen? did i write the command wrong? how can I fix this?

thanks for helping

have a great day
buran wrote Apr-29-2019, 03:20 PM:
Changed thraed title to something meaningful
Please, use descriptive thread titles
Quote
#2
Maybe you need to refresh on slicing and indexing. This [:[[3][::-1]]:-1] could not possibly work. It's not even clear what you try to do.
in more broad sense - you need to iterate over element in the list and if it is a list - reverse also the element. Now ehere comes the question - elements of what type you will get and need to reverse. i.e. you don't reverse sting elements, would you reverse tuples for example?
Quote
#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.
Quote
#4
(Apr-29-2019, 04:00 PM)perfringo Wrote: 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']

Thanks pal
I think that will solve the problem
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  List Comprehension - Creating a list of the length of an item help paul41 2 128 Yesterday, 10:21 AM
Last Post: perfringo
  Writing list as a file, then reading that file as a list Zoastria_Balnala 3 148 Oct-17-2019, 07:54 PM
Last Post: Zoastria_Balnala
  I created a function that generate a list but the list is empty in a new .py file mrhopeedu 2 128 Oct-12-2019, 08:02 PM
Last Post: mrhopeedu
  list approach due nested order 3Pinter 6 132 Oct-07-2019, 01:49 PM
Last Post: 3Pinter
  Creating a list from a comprehension using a list rm197 6 146 Sep-30-2019, 07:02 PM
Last Post: rm197
  Converting List into list of tuples ARV 4 324 Sep-28-2019, 04:58 AM
Last Post: perfringo
  convert list of dict to dict of array of list with pop up kk230689 1 144 Sep-13-2019, 07:29 AM
Last Post: perfringo
  convert List of Dicts into a 2 deep Nested Dict rethink 1 188 Aug-23-2019, 05:28 PM
Last Post: ichabod801
  calculation with list in list gianniskampanakis 13 629 Aug-09-2019, 12:01 PM
Last Post: gianniskampanakis
  removing quotes from a list and keep type list evilcode1 3 225 Aug-03-2019, 11:07 AM
Last Post: perfringo

Forum Jump:


Users browsing this thread: 1 Guest(s)