Python Forum
convert this List Comprehensions to loop - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: convert this List Comprehensions to loop (/thread-38491.html)



convert this List Comprehensions to loop - jacklee26 - Oct-20-2022

Does anyone know how to convert this List Comprehensions to normal for loop code:
results = ['A' , 'B', 'C' , 'D']
]for i in [results[c:c+2] for c in range(0,len(results)) if c%2 == 0]:
    print(*i)
output:
A B
C D


RE: convert this List Comprehensions to loop - deanhystad - Oct-20-2022

The ] before the for loop is an error.

Converting a comprehension to a loop si pretty straight forward. Write the loops backwards.
for i in [results[c:c+2] for c in range(0,len(results)) if c%2 == 0]
becomes
temp = []
for c in range(0, len(results)):
    if c % 2 == 0:
        temp.append(results[c:c+2])

for i in temp:
    print(*i)
I like this way better:
results = iter(["A", "B", "C", "D"])
for i in zip(results, results):
    print(*i)



RE: convert this List Comprehensions to loop - rob101 - Oct-20-2022

As already stated, there's an error (a typo, maybe?)

This is how I would do it:

results = ['A', 'B', 'C', 'D']
for index, c in enumerate(results):
    if index % 2 == 0:
        print(*results[index:index + 2])
Output:
A B C D
I've used index rather than i, but it's the same thing.


RE: convert this List Comprehensions to loop - jacklee26 - Oct-20-2022

Thanks.
It was a typo.
Basely I was trying to find some solution to nth index of a list and add a new line.
For example, if my list has hundreds of data, I just wish new line after 2 columns.
So I went to StackOverflow and found this solution, but I'm unfamiliar with list Comprehension.

Thanks for all the help.


RE: convert this List Comprehensions to loop - jacklee26 - Oct-21-2022

(Oct-20-2022, 04:46 AM)rob101 Wrote: As already stated, there's an error (a typo, maybe?)

This is how I would do it:

results = ['A', 'B', 'C', 'D']
for index, c in enumerate(results):
    if index % 2 == 0:
        print(*results[index:index + 2])
Output:
A B C D
I've used index rather than i, but it's the same thing.

HI rob101,
Is it possible to write it into a text file, instead of print?


RE: convert this List Comprehensions to loop - rob101 - Oct-21-2022

(Oct-21-2022, 01:14 PM)jacklee26 Wrote: HI rob101,
Is it possible to write it into a text file, instead of print?

Yes; you simply add a file handler and redirect the print() function:

with open('output.txt', 'a') as output:
    results = ['A', 'B', 'C', 'D']
    for index, c in enumerate(results):
        if index % 2 == 0:
            print(*results[index:index + 2], file=output)
That will create a file called output.txt within the same directory from which the python script is run.


RE: convert this List Comprehensions to loop - deanhystad - Oct-21-2022

Use range with a step instead of enumerate and modulo.
results = ['A', 'B', 'C', 'D']
for index in range(0, len(results), 2):
    print(*results[index:index + 2])



RE: convert this List Comprehensions to loop - rob101 - Oct-21-2022

I was sticking to the original code (with regard to modulo). Also I've adopted the this practice: [Basic] Never use "for i in range(len(sequence)):

That said, I agree that is a neater solution, but you can drop the 0, right? It's implied.


RE: convert this List Comprehensions to loop - deanhystad - Oct-21-2022

The call is range(stop) or range(start, stop[, step]). If you want to use a step you need to specify the start. All arguments are positional.