Home > Net >  How to print list of tuples without brackets and in different lines python
How to print list of tuples without brackets and in different lines python

Time:11-15

s =[(1, 2), (2, 3), (3, 4), (1, 3)]

Output should be:

1 2

2 3

3 4

1 3

#in python only

"WITHOUT USING FOR LOOP"

In below code

ns=[[4, 4], [5, 4], [3, 3]]
for x in ns:
    n=x[0]
    m=x[1]
    f=list(range(1,n 1))
    l=list(range(2,n 1))
    permut = itertools.permutations(f, 2)
    permut=list(permut)
    s=list(filter(lambda x: x[1]==x[0] 1 , permut))
    #print(s)
    
    m=m-len(s)
    #print(m)
    
    t=list(filter(lambda x: x[1]==x[0] 2 , permut))
    #print(t)
    
    for y in range(0,m):
        s.append(t.pop(0))
        
    print(*s, sep = "\n")

CodePudding user response:

for values in s:
    print(*values) # Simply separates the values so that the print statement puts a space between

This example iterates through and prints each of the tuples as space-delimited strings.

In a more complicated but correct way, you can do:

for values in s:
    print(" ".join([str(v) for v in values])) # Add a space between the values. Note that this requires them to be strings, hence the str(v)

CodePudding user response:

You can do this by using this code snippet below:

s =[(1, 2), (2, 3), (3, 4), (1, 3)]
for i in s:
    print(*i)

CodePudding user response:

You may try this , it could help you.

s =[(1, 2), (2, 3), (3, 4), (1, 3)]

for i in s:
    print(str(i).strip('()'))

CodePudding user response:

This is one way to iterate over a list without using loops

li = [(1, 2), (2, 3), (3, 4), (1, 3)]

res = list(map(lambda x: x, li))
print(*res, sep="\n")
  • Related