Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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]

Using a simple for loop should be enough.

Eg:

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

for label, value in items:
    print label + "," + value
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top