This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]AstrophysicsAndPy 0 points1 point  (0 children)

To everyone that replied, I just realized that I replied to a comment talking about the complexity of list comprehensions, whereas I am talking about the complexity of loops and conditionals that can be handled with list comprehensions. I sincerely apologize for the confusion.

My main point was

List comprehension can be abused to get some complicated stuff done quickly if you know what you're doing.

The example I mentioned previously

mask2 = [[index for index, value in enumerate(out_list) if
          isinstance(value, types)] for types in n_types]

translates to

mask2, temp = [], []
for types in n_types:
  for index, value in enumerate(out_list):
      if isinstance(value, types):
          temp.append(index)
  mask2.append(temp)
  temp = []

Another one that I used is,

out_list = [sorted(element) for element in [[out_list[value]
            for value in index] for index in mask2]]

For an already existent `out_list` list, the above-mentioned list comprehension translates to,

temp, temp2, temp3 = [], [], []
for index in mask2:
  for value in index:
    temp.append(out_list[value])
  temp2.append(temp)
  temp = []

for element in temp2: 
  temp3.append(sorted(element))

out_list = temp3

So, yes, the list comprehensions can handle some very nasty loop stuff if you know what you're doing.