Python Forum
for x in len()? - 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: for x in len()? (/thread-9530.html)



for x in len()? - IAMK - Apr-14-2018

I've got the following code at the moment:
aLen = len(sa_data[1])
for a in range(1,aLen):
Is there a way to simply say: for a in len(sa_data[1]):?


RE: for x in len()? - Larz60+ - Apr-14-2018

for a in range(len(sa_data[1])):
but if sa_data[1] is iterable why not:
for item in sa_data[1]:
where item is your final target.


RE: for x in len()? - IAMK - Apr-15-2018

(Apr-14-2018, 11:26 PM)Larz60+ Wrote:
for a in range(len(sa_data[1])):
Thanks. I realise how stupid the question was. I had already written than into a variable...

As for the second solution, I'm using the number of the elements rather than the data, so the first solution is better for me.


RE: for x in len()? - Larz60+ - Apr-15-2018

No question is a stupid question if you don't know the answer.


RE: for x in len()? - buran - Apr-15-2018

(Apr-15-2018, 12:17 AM)IAMK Wrote: As for the second solution, I'm using the number of the elements rather than the data, so the first solution is better for me.
Actually, no - the first is not better. Please, read https://python-forum.io/Thread-Basic-Never-use-for-i-in-range-len-sequence


RE: for x in len()? - IAMK - Apr-15-2018

(Apr-15-2018, 01:03 AM)buran Wrote: Actually, no - the first is not better. Please, read https://python-forum.io/Thread-Basic-Never-use-for-i-in-range-len-sequence
I find it funny how that's a common mistake for people coming from other languages.

Anyway, thanks. I will replace where range(len()) appears in My Code with enumerate().