quant
quant

Reputation: 4492

how to pass a list as argument in a function in python

I have the following string a = "1.MATCHES_$TEXT$$STRING"

I want to create a function which at some point calls the split function. My function looks like this:

def myfunction(x,splt,sel_nr,col=False):
    if(not col):   
         return(x.split(splt)[sel_nr]

My question is that this: a.split('$')[0:2] works, but this:

myfunction(x=a,splt='$',sel_nr=[0:2],col=False) does not and I do not understand why.

I also tried:

def myfunction(x,splt,*sel_nr,col=False):
    if(not col):   
         return(x.split(splt)[sel_nr]

but it still does not work

I am using python 3.x

Upvotes: 0

Views: 30

Answers (1)

Frank AK
Frank AK

Reputation: 1781

You can't do this! sel_nr is invalid, you can do it with follow way!

def myfunction(x,splt,start, end,col=False):
     if not col:   
          return(x.split(splt)[start: end])

myfunction(x=a,splt='$',start=1, end=3,col=False)

Upvotes: 1

Related Questions