I am trying to write a list with if-else statements in one line. I’ve followed the instructions in this solution, but I got multiple “indent-Expected” errors.
Here is my code:
JavaScript
x
19
19
1
initial= [[1,2,3],[],[]]
2
for block in initial:
3
'''
4
If block is empty move to the next block
5
'''
6
if not block: # empty?
7
continue # change the block
8
elif block: # full?
9
packet = block[0]
10
# True if there is an empty sublist
11
x = [True for i in initial if len(i) == 0]
12
# Enter if there is no empty sublist
13
if not any(x):
14
# Here is the issue
15
if packet > max([continue if sublist == block else sublist[-1] for sublist in initial]):
16
continue
17
block.remove(packet)
18
break # New-one
19
The issue in this line:
JavaScript
1
2
1
if packet > max([continue if sublist == block else sublist[-1] for sublist in initial]):
2
Advertisement
Answer
continue
is a reserved keyword in Python so you are getting the error.
You don’t need a continue
to skip element.
Further an if
without else
is complete in list comprehension
.
Based on comments, if you just need to skip elements you can just use an if
without an else
.
Here is a dummy example to show what you need:
JavaScript
1
5
1
initial = [[1, 2], [2, 3], [4, 5]]
2
block = [1, 2]
3
res = max([sublist[-1] for sublist in initial if sublist != block]) # just if is enough without else
4
print(res)
5