Python Forum
convert this List Comprehensions to loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
convert this List Comprehensions to loop
#1
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
Reply
#2
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)
jacklee26 likes this post
Reply
#3
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.
jacklee26 likes this post
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#4
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.
rob101 likes this post
Reply
#5
(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?
Reply
#6
(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.
jacklee26 likes this post
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#7
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])
Reply
#8
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.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#9
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to convert while loop to for loop in my code? tatahuft 4 834 Dec-21-2024, 07:59 AM
Last Post: snippsat
  convert string to float in list jacklee26 6 3,491 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  convert a list to links Pir8Radio 3 1,940 Nov-28-2022, 01:52 PM
Last Post: Pir8Radio
  Are list/dict comprehensions interpreted really sequentially? anata2047 3 2,271 May-31-2022, 08:43 PM
Last Post: Gribouillis
  Convert list to interger Clives 5 2,599 May-09-2022, 12:53 PM
Last Post: deanhystad
  Convert each element of a list to a string for processing tester_V 6 7,651 Jun-16-2021, 02:11 AM
Last Post: tester_V
  convert numbers into list lokesh 1 3,015 Jun-03-2021, 06:37 AM
Last Post: menator01
Question convert unlabeled list of tuples to json (string) masterAndreas 4 9,227 Apr-27-2021, 10:35 AM
Last Post: masterAndreas
Star Convert Bytearray into List using list() Shlok 2 6,965 Feb-18-2021, 10:44 AM
Last Post: deanhystad
  convert List with dictionaries to a single dictionary iamaghost 3 3,940 Jan-22-2021, 03:56 PM
Last Post: iamaghost

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020