0

I have a list like

L=[[w,['0','0']],[r,['1']]]

I want to print it as

w = 00
r = 1

for getting this I tried like

for k in range(1,len(tab[0]))
    print L[0][k][0],"=",(L[0][k][1])

but it prints like

w = ['0','0']
r = ['1']

can anyone help me? thanx in advance

0

5 Answers 5

1
In [5]: L=[['w',['0','0']],['r',['1']]]

In [6]: L1 = [item for item in L]

In [7]: L1
Out[7]: [['w', ['0', '0']], ['r', ['1']]]

In [8]: ['%s=%s' % (item[0], ''.join(item[1])) for item in L1]
Out[8]: ['w=00', 'r=1']
Sign up to request clarification or add additional context in comments.

Comments

1

If the question is how do I convert ['0', '0'] to 00 then you may use join

for k in range(1,len(tab[0]))
    print L[0][k][0],"=", ''.join((L[0][k][1]))

I don't fully understand the for loop (because I don't know what tab is) but to get string representation of a list, use join

Comments

1

Your list is wrong. Unless w and r are predefined variables, it should read as:

L=[['w',['0','0']],['r',['1']]]

In that case, the following will achieve the result: for item in L:

print(item[0], ''.join(item[1]))

Comments

0

Just use a simple for-loop with string formatting:

L = [['w', ['0','0']], ['r',['1']]]
for item in L:
    print '{} = {}'.format(item[0], ''.join(item[1]))

What ''.join() does it joins every item in the list separated by the '' (i.e nothing). So:

['0', '0'] --> '00'

Comments

0

Go with this..

L=[[w,['0','0']],[r,['1']]]

for item in L:
    print item[0], '=', ''.join(item[1])


Ans:
   w = 00
   r = 1

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.