Deepan Chakravarthi
Deepan Chakravarthi

Reputation: 25

python how to assign 2 values with comma separate for single key in configuration file

I have output like this :

x~y
x~z
y~x
y~z

But I want output like:

x~y,z
y~x,z

This is My Python Code:

allfields=['x','y','z'];

requiredfields=['x','y']

for rf in requiredfields:  
    for af in allfields:
        if rf not in af:
            txt=(rf+" ~ "+af)
            print(txt)

Upvotes: 2

Views: 35

Answers (1)

Mureinik
Mureinik

Reputation: 311498

You could join the values of allfields before printing them:

for rf in requiredfields:
    txt = rf + "~" + ",".join(a for a in allfields if a not in rf)
    print(txt)

Of course, you could also using join to collapse the outer loop:

print("\n".join(rf + "~" + ",".join(a for a in allfields if a not in rf) \
for rf in requiredfields))

Upvotes: 2

Related Questions