1

I have tupled list like this.

[('"ram', '18"'), ('"kp', '12"'), ('"nm', '14"')]

How to unpack this to get the result like below.

ram,18
kp,12
nm,14

Thanks.

Steve Barnes
  • 27,618
  • 6
  • 63
  • 73
  • Refer [http://stackoverflow.com/questions/7839168/extracting-information-from-a-tuple-python](http://stackoverflow.com/questions/7839168/extracting-information-from-a-tuple-python). – Visruth May 01 '14 at 08:02

3 Answers3

1

You can just iterate over the list to unpack each piece.

mylist = [('"ram', '18"'), ('"kp', '12"'), ('"nm', '14"')]
for tup in mylist:
  print ",".join(tup)

Output:

"ram,18"
"kp,12"
"nm,14"

If you do not like the quotes, just remove them after the join.

for tup in mylist:
  print ",".join(tup).replace('"','')

Output:

ram,18
kp,12
nm,14
merlin2011
  • 71,677
  • 44
  • 195
  • 329
0
ta = [('"ram', '18"'), ('"kp', '12"'), ('"nm', '14"')]
for t in ta:
    print ','.join(t)

or you can access individual items by indexing them:

ta[1]
Steve Barnes
  • 27,618
  • 6
  • 63
  • 73
0

Using a simple for loop should be enough.

Eg:

items = [('ram', '18'), ('kp', '12'), ('nm', '14')]

for label, value in items:
    print label + "," + value
KBN
  • 2,915
  • 16
  • 27