for example I don't know what this is called » [('cat', 'meow'), ('dog', 'ruff')]
if I try for l in [('cat', 'meow'), ('dog', 'ruff')]: print l I am using l for lady luck lucy not I for I or the number 1 + maybe even | pipe for smoking
it makes it like this ↴

('cat', 'meow')
('dog', 'ruff')

so I can now remove the square brackets but I want to make it liek this ↴

catmeow
dogrugg

if there are spaces in the words then I will put them there first before I begin but I would also like to know how to make it like this other way just incase I need in my future ↴

cat meow
dog ruff

and even like this ↴

cat, meow
dog,ruff

Dani AI

Generated

That structure is a list of 2-tuples (pairs) — as said. was right to suggest unpacking variable names, and correctly pointed out using join when you want a separator. Below are compact, practical patterns that avoid repeating the exact examples already in the thread and show safe options for different output styles.

For simple prints (concatenated, space, comma) using tuple unpacking and str.format (works on Python 2/3; f-strings are an alternative on Python 3.6+):

pairs = [('cat','meow'), ('dog','ruff')]

for a, b in pairs:
    print("{}{}".format(a, b))    # catmeow
    print("{} {}".format(a, b))   # cat meow
    print("{},{}".format(a, b))   # cat,meow

If you want to build the lines first (e.g., to write to a file or reuse them), a list comprehension plus a newline join is convenient:

lines = ["{} {}".format(a, b) for a, b in pairs]
print("\n".join(lines))

Notes and edge cases: make sure tuple items are strings — format will convert numbers, but if items may be None or custom objects, convert explicitly (e.g., str(x)) or sanitize first. For comma-separated output that must be correct when values contain commas or quotes, use the csv module so fields are quoted and escaped properly:

import csv
with open('pairs.csv', 'w', newline='') as f:
    csv.writer(f).writerows(pairs)

Finally, be aware of print syntax differences between Python 2 (statement) and Python 3 (function), and choose f-strings for brevity if running Python 3.6+. The thread examples and the approaches above cover most common needs: quick display, building reusable strings, and safe CSV output.

Recommended Answers

All 4 Replies

for example I don't know what this is called » [('cat', 'meow'), ('dog', 'ruff')]

It's called a list of tuples

so I can now remove the square brackets but I want to make it liek this ↴

catmeow
dogrugg

You can do something like this:

your_list = [('cat', 'meow'), ('dog', 'ruff')]
for tpl in your_list:
    print tpl[0]+tpl[1]

You can also use for animal, noise in instead of for tpl in. Then you can use animal and noise instead of tpl[0] and tpl[1] respectively.

commented: thanks :) +4

Many thanks for your response but unfortunately I cease to recall what this is for but it has renewed my learning interest in pythons again. :)

Another option ...

mylist = [('cat', 'meow'), ('dog', 'ruff')]
for tup in mylist:
    print(" ".join(tup))  # use "" for no space
commented: thanks :) +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.