4

I need some help in Python, to print:

I have:

input =[(3, 'x1'), (5, 'x3'), (2, 'x2')]

need to print, in this form:

x1=3 x2=2 x3=3

Many thanks

1
  • Is there a need to be the list of x's sorted? Commented Dec 5, 2010 at 15:38

3 Answers 3

9
print ' '.join('%s=%s' % (k, v) for (v, k) in input)
Sign up to request clarification or add additional context in comments.

Comments

4
for x,y in input: 
    print "%s=%s" % (y, x),

1 Comment

or print ' '.join("%s=%d" % (y, x) for x,y in input)
1
input =[(3, 'x1'), (5, 'x3'), (2, 'x2')]     
for i,j in input:
   print("{}={}".format(j,i),end=" ") 
   

print j,i because second one will come before first. Format is function used to print the values. If u want to print above in different lines rather than single then remove end=" "

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.