if/else condition in a list comprehension
I am trying to do an if/else with a list comprehension (I think). I tried to follow a normal if conditional format with the if/else at the end. I tried to add 'num for num in nums'. A few other things like the commented code at the bottom...but the best I can do is make res[] a list of 'None's. I'm sure this will never be crucial and a normal if/else in a for loop works, it's just that I read Trey Hunner's visual take on List Comps and wondered about an else. nums = [2,53,4,7,11,1] print(nums) res = [] for num in nums: if num <= 7: res.append("less") else: res.append("more") print(res) nums = [2,53,4,7,11,1] print(nums) res = [] res = [res.append("less") if num <= 7 else res.append("more") for num in nums ] print(res) # [a if tC else b for i in items if fC] # [item # for sublist in myList # for item in (sublist if type(sublist) is list else sublist['val']) #] python-3.x