2

I have a line of code like this:

list1=[string1[i:i+int1] for i in range(0, len(string1), int1)]

I remember my teacher saying that we should start new lines when there is 'for' so, is there a way to write this code that looks like:

for i in range(0, len(string1), int1):
    #something here

or something else?

Wayne Werner
  • 49,299
  • 29
  • 200
  • 290
Jake
  • 159
  • 4
  • 8
    Please read about [list comprehensions](https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions). – Lafexlos Apr 04 '16 at 14:13
  • 8
    Your teacher's advice is an oversimplification. You should start a new line when there is a for _statement_, but a `for` inside of an expression isn't part of a for statement; it's part of a list comprehension. – Kevin Apr 04 '16 at 14:14
  • 3
    I'm almost certain your teacher didn't mean you should forgo all [list comprehensions](https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions) in favor of explicitly `for` loops that build a `list` piecemeal. If they did, they aren't really teaching you Python. – ShadowRanger Apr 04 '16 at 14:15
  • No doubt your teacher was trying to simplify things. I'll update your title to the question that you're actually asking here. – Wayne Werner Apr 04 '16 at 14:20

5 Answers5

5

You mean to extract a boring old regular for loop from a list-comprehension?

list1=[string1[i:i+int1] for i in range(0, len(string1), int1)]

Becomes:

list1 = list()
for i in range(0, len(string1), int1):
    list1.append(string1[i:i+int1])

This would be useful if you wanted to add exception handling, logging, or more complex functions or behaviors while you iterate over your data.

For instance:

list1 = list()
for i in range(0, len(string1), int1):
    log.info('in loop: i={}'.format(i))
    try:
        data = string1[i:i+int1]
    except:
        log.error('oh no!')
        # maybe do something complex here to get some data anyway?
        data = complex_function(i)
    log.debug('appending to list: data={}'.format(data))
    list1.append(data)

But generally speaking the list-comprehension is a totally legitimate way to write that.

Inbar Rose
  • 41,843
  • 24
  • 85
  • 131
  • alternatively, one could write a function and do `[somefunction(val) for val in values]`. Assuming that was appropriate for the case, of course. As another potentially appropriate alternative, rather than returning a list, one could `yield data` and get a generator out of the deal. – Wayne Werner Apr 04 '16 at 14:25
  • 4
    `[]` is more efficient than `list()`. See [why](https://stackoverflow.com/questions/30216000/why-is-faster-than-list/30216156#30216156) – zondo Apr 04 '16 at 14:25
  • 4
    @zondo: Also conveniently avoids the problem of people name-shadowing `list`, though ideally, they wouldn't do that in the first place. :-) That said, the incremental cost of `list()` over `[]` is pretty small, about 60-70 extra ns on my machine (admittedly an increase of 2-3.5x over the base cost of `[]` of ~20-30 ns, but unlikely to matter in all but the hottest of loops). – ShadowRanger Apr 04 '16 at 14:50
  • I'd add readability to that. `something = []` is more immediately recognisable than `something = list()` to me. – Score_Under Jan 14 '18 at 00:48
3

You have to create the empty list first, and then append for each iteration.

list1 = []
for i in range(0, len(string1), int1):
    list1.append(string1[i:i+int1])
Avión
  • 7,963
  • 11
  • 64
  • 105
1

That list comprehension would translate to:

l = []
for i in range(0, len(string1), int1):
    l.append(string1[i:i+int1])
heinst
  • 8,520
  • 7
  • 41
  • 77
0

Little bit dirty, but a good alternative for splitting collection to subcollections of a fixed size.

from itertools import zip_longest

l = []
for chunk in zip_longest(*([iter(string1)]*int1), fillvalue=''):
    l.append(''.join(chunk))
AndreyT
  • 1,449
  • 1
  • 15
  • 28
0
g = (string[i:i+int1] for i,char in enumerate(string) if i % int1 == 0)
l = list(g)

Do not use len(iterable) in the while dealing with indices. Use enumerate() for that. It yields (index, item). This is efficient and pythonic.

The idea that you should make a newline while using for loop is probably idiomatic to some other language, C, C++ e.g. But not in python. List comprehensions come fro the Haskell world of FP. So that's elegant as they come.

C Panda
  • 3,297
  • 2
  • 11
  • 11