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
#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


Messages In This Thread
RE: convert this List Comprehensions to loop - by jacklee26 - Oct-21-2022, 01:14 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  convert string to float in list jacklee26 6 2,026 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  convert a list to links Pir8Radio 3 1,168 Nov-28-2022, 01:52 PM
Last Post: Pir8Radio
  Are list/dict comprehensions interpreted really sequentially? anata2047 3 1,521 May-31-2022, 08:43 PM
Last Post: Gribouillis
  Convert list to interger Clives 5 1,697 May-09-2022, 12:53 PM
Last Post: deanhystad
  Convert each element of a list to a string for processing tester_V 6 5,492 Jun-16-2021, 02:11 AM
Last Post: tester_V
  convert numbers into list lokesh 1 2,441 Jun-03-2021, 06:37 AM
Last Post: menator01
Question convert unlabeled list of tuples to json (string) masterAndreas 4 7,591 Apr-27-2021, 10:35 AM
Last Post: masterAndreas
Star Convert Bytearray into List using list() Shlok 2 4,338 Feb-18-2021, 10:44 AM
Last Post: deanhystad
  convert List with dictionaries to a single dictionary iamaghost 3 2,927 Jan-22-2021, 03:56 PM
Last Post: iamaghost
  Convert string to JSON using a for loop PG_Breizh 3 3,035 Jan-08-2021, 06:10 PM
Last Post: PG_Breizh

Forum Jump:

User Panel Messages

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