4

I am trying to implement a ternary conditional operator in a list-comprehension. I have written it like this:

lst.append(dict2obj(item)) if type(item) is not in ['int'] else lst.append(item) for item in v

Where lst is empty list and v is another list with various elements. Editor is showing it syntactically incorrect. What am I doing wrong?

3
  • remove the is (no such keyword in Python to the best of my knowledge) Commented Jan 16, 2014 at 8:58
  • Sure there is. You can do x is y. It is is in that does not exist. Commented Jan 16, 2014 at 8:58
  • Using list comprehensions for calling functions inside is not easy to read, furthermore in a one-liner. Moreover, if lst is empty, @InbarRose' answer is clearly the best. Commented Jan 16, 2014 at 9:00

3 Answers 3

6
  • If you mean to write list comprehension you missed [, ]:
  • There is no is not in operator. Use not in.
  • type function does not return string. Why not use isinstance(item, int)?

[lst.append(dict2obj(item)) if not isinstance(item, int) else lst.append(item)
 for item in v]

Use simple for loop if possible. It's more readable.

for item in v:
    if not isinstance(item, int)
        lst.append(dict2obj(item))
    else:
        lst.append(item)
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for the answer. I did modify it like this but still it is showing it syntactically incorrect.
This will give a list of None since list.append does not return anything. Why would you want that?
@HannesOvrén When life gives you Nones... hey, a free bunch of Nones! Moreover, you'll have your lst made, which is why you'd probably want list.append (in a comprehension or beyond) or another of the many methods that mutate collections in place and return None.
@NunoAndré I don't have a problem with list.append, but with the use of list comprehension to do it. Unless the interpreter is smart enough to not compute the result of the list comprehension (which I doubt that CPython is) you are creating a potentially huge list that will require memory, but will never be used. It's also less readable, although I guess that is subjective.
1

If the lst is empty from the start, you can simply create it like this:

lst = [dict2obj(item) if not isinstance(item, int) else item for item in v]

If you already have the list and want to add items, the proper way to do this in Python is to just extend the list you have with the new list:

lst.extend([dict2obj(item) if not isinstance(item, int) else item for item in v])

Or something like this (this uses an generator) to prevent extra overhead:

map(lst.append, (dict2obj(item) if not isinstance(item, int) else item for item in v))

1 Comment

lst is said to be empty, why not directly do something like lst = [dict2obj(...)]?
1

I avoid mixing list comprehensions with ternary operators because it's too hard to understand what the function does at a glance.

I also try to use list comprehensions only for building up a list of return values. If I desire side-effects (such as adding items to a list), I will do this in a regular for-loop. This is especially true when I don't care about the list of return values. If you do go the route of a list comprehension, use the consume recipe for itertools (http://docs.python.org/2/library/itertools.html#recipes). consume((lst.append(dict2obj(item)) if not isinstance(item) else lst.append(item) for item in v), None)

Here's how I'd solve this problem if I didn't do use @falsetru's approach (which is probably the easiest to read)

def convert(item):
    if not isinstance(item, int):
        result = dict2obj(item)
    else:
        result = item
    return result

lst.extend(map(convert, v)) #or itertools.imap

convert could be a lambda function if you're willing to trade compactness for readability.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.