2

How do I split tuple from dictionary

dic = {('K30', 'K56'): 1}

to get this output in a text file:

K30    K56    1

what I tried is

for k,v in dic.items():
    a,b = k.split(',') 
    print >>f, a+'\t',b+'\t',v
f.close()

but I got the error:

AttributeError: 'tuple' object has no attribute 'split'

4 Answers 4

5

You need not split you can simply say

 a,b = k
Sign up to request clarification or add additional context in comments.

1 Comment

or for (a, b), v in dic.items():
1

Like this:

for k,v in dic.items(): 
print >>f, k[0]+'\t',k[1]+'\t',v f.close()

Just access the tuple elements.

Comments

1
for k,v in dic.items():
    print '\t'.join(k),'\t',v

Comments

0

Since all the good ones are taken, here goes a concise version

[print(k[0],k[1],v) for k,v in dic.items()]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.