Matt Norris
Matt Norris

Reputation: 8836

How do I get same result without using enumerate?

I do not need char in this example, but I include it to get my desired results.

charlist = [strval[0:count+1] for count, char in enumerate(strval)]

How do I get the same result without using enumerate?

Upvotes: 1

Views: 107

Answers (3)

Federico Cáceres
Federico Cáceres

Reputation: 1216

How about replacing enumerate(...) with zip(xrange(...),...)?

[strval[0:count+1] for count, char in zip(xrange(len(strval)),strval)]

Upvotes: 0

pyfunc
pyfunc

Reputation: 66729

If you do not want to use enumerate, use range since all you want is count value

>>> strval = "abcd"
>>> for count, char in enumerate(strval): print count, char
... 
0 a
1 b
2 c
3 d
>>> for count in range(len(strval)): print count
... 
0
1
2
3
>>>

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799110

xrange(len(strval))

Upvotes: 6

Related Questions