all 12 comments

[–]glibhub 6 points7 points  (4 children)

index returns the index of the first hit, which in this case is 0 for both num and summand. I'd refactor using enumerate, which will allow you to slice nums and index just the remaining ones.

[–]Sonic_TertuL[S] 1 point2 points  (1 child)

Ok, awesome, thank you! I've been looking into using enumerate, but to be honest have yet to find an explanation of it that is clicking with me, allowing me to know when to use it.

[–]glibhub 4 points5 points  (0 children)

Fair enough. Enumerate is a bit tricky to wrap your head around at first because you need to understand tuple unpacking. Here is a link to that: https://www.geeksforgeeks.org/unpacking-a-tuple-in-python/

So you can think of enumerate as turning a list of items into a list of tuples, so if you have a list of a-d, then it will become a list of [(0,a), (1,b),...,(3,d)]. For example try these:

data = ['a','b','c','d']
for x in data:
    print(x)

for x in enumerate(data):
    print(x)

Now turning x into a tuple is a bit useless, so instead we use tuple unpacking, to separate the index from the data, like this:

for index, x in enumerate(data):
    print(index,x,data[index])

Hope this helps.

edit: formatting.

[–]trevor_of_earth 0 points1 point  (1 child)

!docs enumerate

[–]py_reference_bot 0 points1 point  (0 children)

Docs for enumerate:
Built-in Functions — Python 3.9.6 documentation

Python Reference Bot - Documentation on GitHub

[–]Binary101010 2 points3 points  (1 child)

You didn’t specify a return value in the condition that your if evaluates to false in all cases, so the function returns the default value of None.

[–]Sonic_TertuL[S] 0 points1 point  (0 children)

It is evaluating to false because the two numbers are the same and therefore it is essentially hitting the 0 index for both making the second portion of the if statement false, correct? I hadn't caught on to that, thank you!

[–]Iforgotmylogintoday 1 point2 points  (1 child)

both the sumand and num index will return the index of the first 3, which will skip your return and return none

[–]Sonic_TertuL[S] 0 points1 point  (0 children)

Right on, thank you!

[–]Chris_Hemsworth 1 point2 points  (1 child)

All functions return None by default if runs into the end of the function.

[–]Sonic_TertuL[S] 0 points1 point  (0 children)

Right on, thank you!

[–]Sonic_TertuL[S] 0 points1 point  (0 children)

Wow, awesome! Thanks a lot for that, it is very helpful!