Alitai
Alitai

Reputation: 3

Print list of items line by line in a file

Is it possible to Print the below Result line by line?

I tried to add \n on print(final \n, file=f). But that's not working.

Here is the Code:

def calcperm(arr, size):
    result = set([()])
    for dummy_idx in range(size):
        temp = set()
        for dummy_lst in result:
            for dummy_outcome in arr:
                if dummy_outcome not in dummy_lst:
                    new_seq = list(dummy_lst)
                    new_seq.append(dummy_outcome)
                    temp.add(tuple(new_seq))
        result = temp
    return result

lst = [1, 2, 3, 4]
seq = 3
final = calcperm(lst, seq)
print(len(final))
with open('file.txt', 'w') as f:
    print(final, file=f)

Upvotes: 0

Views: 46

Answers (1)

Rory Daulton
Rory Daulton

Reputation: 22544

Replace your last two lines (the with structure) with this:

with open('file.txt', 'w') as f:
    for item in final:
        print(item, file=f)

This will print all 24 tuples in final to your file, line by line.

Upvotes: 1

Related Questions