how to handle null argument in python

1st Scenario
Currently have a python script which accepts 1 argument and does something. nyc:denver:newjercy

Inside my

2nd Scenario:
Would like to introduce and pass 2nd argument state_list=sys.argv[2].split(':')

e.g nyc:denver:newjercy ny:ct:ny:co

Inside my

Upto this stage it works fine.

But now
if I dont pass 2nd argument to my it fails with below Error. Which I understand bcuz I'm not passing the argument.
  IndexError: index out of range: 2

  Request is:
  How to handle 2nd argument inside python script. If I pass 'none', is it easy to handle? pls help with sample script.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Duy PhamFreelance IT ConsultantCommented:
I think you can simply use try/except block for 2nd argument, for example:
except IndexError:
    # if 2nd argument is not passed, it goes here

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
enthuguyAuthor Commented:
Thanks for your quick help
I suggest the following
state_list = sys.argv[2].split(':') if len(sys.argv) >= 3 else []

Open in new window

This is called a conditional expression, and works the same way as
if len(sys.argv) >= 3:
    state_list = sys.argv[2].split(':')
    state_list = []

Open in new window

But the conditional expression is better here. It is more dense (fewer lines), the state_list is not repeated (less error-prone as you cannot create two variables by typo error). Actually only the first part of the expression is the important one (this is what should be done); so, you can skip the rest if you are re-reading the source to get the idea.

It does not matter (what solution you choose) too much for getting the sys.argv if the program is short. If there is more complex processing of command-line arguments, then have a look at the argparse standard module (
enthuguyAuthor Commented:
Thanks pepr, this is a good one too
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.