Skip to content Skip to sidebar Skip to footer

Understanding The Syntax Of List Comprehensions (and Nested Lcs)

This works: allDasTickets = ['9255955', '9255958', '9255960', '9255977'] [[j for j in allDasTickets if x != j] for x in allDasTickets] ['9255958', '9255960', '9255977'] ['9255955'

Solution 1:

For list comprehensions, the syntax is

[ <something> for elem in sequence]

Where <something> can itself be a list comprehension. Meanwhile, for nested loop (but not nested lists) comprehensions, the syntax is

[<something> forelemin outerseq forelem2in innerseq]

In the same way, and so on.

Post a Comment for "Understanding The Syntax Of List Comprehensions (and Nested Lcs)"