WhyWhat
WhyWhat

Reputation: 250

How do I conditionally filter the first item in a python list?

I have the following function:

def filterArgs( args ):
    filterflag = len(args)>=1 and args[0] == "-i"
    if flag:
        args = " ".join(args[1:]).strip()
    else:
        args = " ".join(args).strip()

In my code I call it like this:

filterArgs( [ 106645929 ] )       #example 1
filterArgs( [ "-i", 106645929 ] ) #example 2

Is there a way to use something like a conditional operator in C and ignore the func filterArgs?

args = filterflag ? args[1:] : args

My objective is to write less lines.

Upvotes: 1

Views: 191

Answers (1)

ninjagecko
ninjagecko

Reputation: 91094

in python, C's cond ? iftrue : iffalse translates to iftrue if cond else iffalse

thus, args = args[1:] if filterflag else args

Upvotes: 4

Related Questions