0

I'm practicing nested lists comprehension and i encountered some problems i cannot solve nor find any solutions online :<

nested_lista = [[2,1,2,3],[1,2,3,4],[4,4,[16,1,3]]]

Using loops its easy to iterate through every layer of this nested list

def nested_loops():
    for x in nested_lista:

        for y in x:
            print(y)

            if type(y) == list:

                for z in y:
                    print(z)

Output:

2
1
2
3
1
2
3
4
4
4
[16, 1, 3]
16
1
3

Now im trying to achieve similar output with nested list comprehension but it's not working no matter what i try ;/

Here's what I've come up with:

[[[print(y) for y in z if type(z)==list]print(z) for z in x]for x in nested_lista]

or at least i tried to iterate through last layer but it also doesnt work

[[[print(y) for y in z if type(z)==list] for z in x]for x in nested_lista]

Is it possible to solve this or i should give up?

Szejm
  • 37
  • 1
  • 5
  • List comprehensions aren't just a nifty way of writing `for` loops; they're for *building lists*. If you want to cause side effects rather than building a list, a list comprehension is not the tool for the job. – user2357112 Aug 07 '18 at 01:27
  • @user2357112supportsMonica This is a reasonable question: python lacks chaining for collections processing - putting it behind every other language that I have used recently including javascript, ruby, java, scala, and R. . Then what ? Loops are pre 2009 for many of us who had learnt a functional programming language. Comprehensions are legitimate collections processing (though kinda backwards and without chaining) . So it makes sense to explore the boundaries of what they can do. Asking about them makes total sense when python is the tool we need to use. – WestCoastProjects Mar 05 '21 at 15:06
  • @StephenBoesch: Chaining is not a goal in and of itself, and certainly not a goal worth building a bunch of garbage lists for. – user2357112 Mar 05 '21 at 16:35
  • @user2357112supportsMonica "Garbage lists". This comment reflects a poor understanding of the data processing pipelines domain (whether or not you have that experience it is not shown in the comment). See also https://stackoverflow.com/questions/49001986/left-to-right-application-of-operations-on-a-list-in-python-3. Chaining provides an easy to read and efficient means to perform a series of operations. A dozen operations can be shown in a dozen lines: clear to read, quick to write and maintainable. – WestCoastProjects Mar 05 '21 at 17:20
  • @StephenBoesch: I don't know what you consider a giant, unused, triply-nested list of `None`s to be if not junk. – user2357112 Mar 05 '21 at 17:28
  • @user2357112supportsMonica The question is imo exploring how to go about dealing with the limitations of python wrt collections processing. The code itself is a toy example (the data is not used . So what ?) That is not the point but rather : how can we adjust to the constraints within the python language for performing anything more involved than low single digits transformations? Must we break them up into separate named functions - even if they are only used once in this one place? I welcome seeing investigations in how to better handle this. – WestCoastProjects Mar 05 '21 at 17:34
  • @StephenBoesch: I think you're reading things into the question that aren't there. The questioner was explicitly practicing nested list comprehensions, not searching for ways to concisely perform series of data transformations. – user2357112 Mar 05 '21 at 17:42
  • @user2357112supportsMonica That's possible. I've been hyper focused on this topic recently – WestCoastProjects Mar 05 '21 at 18:18

1 Answers1

0

With a fresh mind after 2 weeks holiday it took me +-30 min to get the answer.

Ready for more nests:

answer = [[[print(y) if type(y) is not list else print(x) for x in y] if type(y) == list else print(y) for y in z]for z in nested_lista]

shorter one with same result:

answer2 = [[[print(x)  for x in y] if type(y) == list else print(y) for y in z]for z in nested_lista]

Output:

2
1
2
3
1
2
3
4
4
4
16
1
3

Not sure why I got the " - " though.

Szejm
  • 37
  • 1
  • 5
  • Nice to see you got through this. If it seemed hard and a lot of work? Well it really is both of those. Vanilla python is a poor language for functional programming since it lacks support for piping of results between stages in a chained manner and lambdas do not support multi-lines or statements. There are some libraries to lessen the blow: take a look at `fluentpy`, `JulienPalard/pipe`, `infixpy` and others. Some options are shown in my Q&A here https://stackoverflow.com/a/49018515/1056563 – WestCoastProjects Mar 05 '21 at 15:11