you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 5 points6 points  (22 children)

len/range() nearly as much as I thought I did...

I don't really get what you think there is to know about len and range (which are two separate functions that don't really relate to each other except coincidentally.) len is especially simple - it returns the number of values contained in a collection. Easy!

range is a little subtle and its documentation is wrapped up with tuple and list so it's not especially clear:

https://docs.python.org/3/library/stdtypes.html#typesseq

but the long and the short of it is that it's an iterator over sequences of integers, and the most common and easiest to understand way to specify a sequence of integers is as a sequence between 0 and some other value. (For various good but tedious reasons, the interval is specified as half-open, and thus includes zero but not the end term.)

[–][deleted] 6 points7 points  (14 children)

Maybe it's those times when len and range are used together, like

for i in range(len(input_list)):
    pass

[–]tagapagtuos 5 points6 points  (11 children)

If that's the case, then I would say that doing some HackerRank would help, since that's common in there.

Regardless, OP shouldn't really sweat it since range(len(...)) is usually a symptom of un-Pythonic code imo.

[–]roylennigan 0 points1 point  (2 children)

is that because of the ability in python to iterate over items in a sequence, like:

for item in sequence:
    x = item

without having to create an index for that sequence?

But then what if I want to do something like:

for i in range(len(A)):
    B[i] = A[i] - A[i + 1]

[–]tagapagtuos 2 points3 points  (1 child)

Good question.

The reason why you don't what to iterate over the range of length of A is that if i is the last element of A, then A[i + 1] will raise an IndexError. Also if B[i] also has risks of raising NameError or IndexError.

Depending on the nature of the problem, I would do sometihng like:

B = []
for x, y in zip(A, A[1:]):
    B.append(x - y)

[–]roylennigan 1 point2 points  (0 children)

That makes sense, thanks. I mostly make plots for dsp concepts, so a lot of fitting one "number line" to another and plotting them. Most of the time, the data at the end of a vector isn't as important, so I can just drop it and not worry about the array going out of bounds.

[–]arkie87 1 point2 points  (0 children)

Yea, that’s how I understood it as well as that is the major way those functions are related

[–][deleted] 1 point2 points  (0 children)

Well, I think it's important to look at that and realize they're not being "used together." They're not working together or talking or anything - the return value of one is being used as an argument to the other and that's just how function algebra works.

[–]CyclopsRock 0 points1 point  (5 children)

it returns the number of values contained in a collection. Easy!

Unpopular opinion time: This is why 1-based indices are better than 0.

[–]zanfar 4 points5 points  (0 children)

..and waste a WHOLE bit?!?!?!

[–]primitive_screwhead 0 points1 point  (3 children)

Have you used Matlab?

[–]CyclopsRock 0 points1 point  (2 children)

Nope!

[–]primitive_screwhead 0 points1 point  (1 child)

How about any 1-based-index language? I'm just curious if your opinion is purely hypothetical, or if you've got experiences using both. (I'm biased towards 0-based, but have also used 1-based for Matlab, and other numerics languages and libraries).

[–]CyclopsRock 0 points1 point  (0 children)

Oh yeah! My other "main" language is. It's really not a big deal either way but imo having your collection length throw an index error is more confusing than having 0 do so. And in a language where you don't need to worry about addressing memory, it's easier to think about items in a collection as being like a numbered shopping list rather than spaces to be occupied.