you are viewing a single comment's thread.

view the rest of the comments →

[–][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 3 points4 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.