Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use comma separated variable?

Tags:

python

I have a variable like this: ignore = val1,val2

But it's unclear for me how to use these values as separate ones.

Currently (with my knowledge) i need to hard code them like code below:

if (not Path.find("val1") > -1 ) and (not Path.find("val2") > -1 ):
    etc

Now i want test added to it, and again i need to hard code it like this:

if (not Path.find("val1") > -1 ) and (not Path.find("val2") > -1 ) and (not Path.find("test") > -1 ):

Isn't there a better way of doing this?

like image 536
user1987032 Avatar asked May 10 '26 21:05

user1987032


2 Answers

If ignore is a tuple of value names:

if all(Path.find(v) <= -1 for v in ignore):

This has the advantage to stop as soon as the first condition is false. Just like your hard-coded example.

like image 117
eumiro Avatar answered May 12 '26 12:05

eumiro


This is a tuple, one of the basic datatypes in Python.

You can access the different values using indexing notation, like ignore[0], ignore[1], etc.

However, if you're struggling with fundamental language features like this, I'd strongly recommend that you go read a Python tutorial before continuing.

like image 38
Antimony Avatar answered May 12 '26 11:05

Antimony



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!