Reputation: 1975
Can anyone correct this? See where I'm going wrong?
row_length = 4
ic = 1
pc = 2
newlist = [('') if not x == ic and not x == pc else ('RESERVED ID')
if x == ic ('RESERVED PARENT') if x == pc for x in range(row_length)]
Should end up with:
newlist = ['RESERVED ID', 'RESERVED PARENT', '', '']
Upvotes: 0
Views: 1356
Reputation: 56587
Is this what you are looking for?
newlist = [''] * row_length
newlist[ic-1] = 'RESERVED ID'
newlist[pc-1] = 'RESERVED PARENT'
Not everything has to be done in a list comprehension. My code is not only more efficient but what is more important is easy to read.
EDIT Here's your one-liner. But please, don't! Be kind to potential people that will read your code some day:
newlist = ['RESERVED ID' if idx == ic-1 else 'RESERVED PARENT' if idx == pc-1 else '' for idx in range(row_length)]
Upvotes: 1