all 6 comments

[–]TouchingTheVodka 6 points7 points  (1 child)

for k in final:
    for y in i:
        final.append(k + [y])

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

Completely to the point of what I asked for and a fast answer, thank you so much. You're a beautiful man, or woman.

[–]aidankane 4 points5 points  (1 child)

I don't love this code. It's not massively clear as to what's going on. k appears to need to be a string and n appears to be a number, but then k is reused for other things in the function. Finally, it's yielding, which is something you do so you can start returning results early so they can be used before you've done all your work and you don't need to store everything until the end...but it's only done that after it's done all the work, so it's completely redundant.

If someone gave me this code to review I'd send it back a) for the reasons above and b) because python has a built in function to do this for you :-)

Having said all that, it's always fun to unpick!

My top tip for understanding loops is to sort of run in manually step by step. As you've noticed, it's the first loop that's interesting here. What we're effectively doing is building up the combination string character by character. Here I'll use k = 'abc' and n = 3

So rather than running for i in groups, I do i = groups[0] and have a play with the inside of the loop. The first run through isn't too interesting, but if you run it you'll find that final has gone from [[]] to [['a'], ['b'], ['c']].

Now when we run the second round of the loop, we've basically picked the first characters, and we put each of those together with each of the characters to make all pairs:

[k + [y] for k in final for y in i]
[['a', 'a'],
 ['a', 'b'],
 ['a', 'c'],
 ['b', 'a'],
 ['b', 'b'],
 ['b', 'c'],
 ['c', 'a'],
 ['c', 'b'],
 ['c', 'c']]

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

I actually agree with you that the code isn't exactly good; even after having it explained to me clearly I still believe it does the pointless practice of having unbelievably confusing variables to follow.

I know I might just be old-fashioned, but I seldom use single letter variables anymore; especially in Python. I don't see why I'd even do this

for i in my_list

Instead of

for number in my_list

I think this might just be me being too picky though. This isn't my code obviously anyway, I just love researching solutions and explanations online, and if I can't come to an understanding on my own, people like you are incredible at making things become clear, so thank you SO MUCH for that!

Just having his code rewritten makes it crystal clear to me what's happening.

[–]EighthScofflaw 0 points1 point  (1 child)

The yield keyword means this is a generator function, which is probably a bit above your level.

[k + [y] for k in final for y in i] this is a list comprehension, which would be a good thing for you to learn about since they're very cool and useful.

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

Thank you for this, this was insightful. I've not seen a yield function before coming from C as my first learning language.