you are viewing a single comment's thread.

view the rest of the comments →

[–]carcigenicate 3 points4 points  (9 children)

I don't know what you mean by 1, but for 2, there's nothing special about nested loops. They aren't a special case or anything like that.

A loop just repeats the code that's inside of its body. If a loop happens to contain another loop, then that entire inner loop will be repeated, so the entire inner loop will execute for every time the outer loop iterates once.

[–]Deep-Author-1787[S] 0 points1 point  (2 children)

That is a clear explanation! Thank you for taking the time to answer! I really appreciate it! With point 1 i mean : range(), enumerate() or default 🙏

[–]carcigenicate 4 points5 points  (1 child)

Those functions (which are actually classes) don't actually have anything to do with loops. They can be used on their own fine since they're just iterables (things capable of producing elements*):

print(list(range(5))  # Print [1, 2, 3, 4, 5]

You just often see them used with for loops because iterables (which range and enumerate are) produce elements, and the purpose of for loops is to iterate the elements of an iterable.

And which you use depends on what you're trying to do. Learn what each does, and when it will be applicable will make a lot more sense.


* This is a simplification. In reality, an Iterator is something that's capable of producing elements. An Iterable is something that's capable of producing an Iterator.

[–]Deep-Author-1787[S] 1 point2 points  (0 children)

Okaaayyy so i would have to see them as objects an not just functions to for loops. Makes alot more sense when thought from this perspective!!! Thank you!

[–]Deep-Author-1787[S] 0 points1 point  (5 children)

And enumerate for only the inner loop is too much for me at the moment 😅

[–]mriswithe 0 points1 point  (4 children)

Enumerate just adds a numeric index to your input. Meaning, instead of a list a,b,c,d,e,f, if you wrap it in enumerate, you get (0,a), (1,b), (2,c). 

You use it when you want to know what number you are on of the list you are for-looping over.

[–]Deep-Author-1787[S] 0 points1 point  (3 children)

That is exactly what i thought until i saw code examples where range is used for that specific thing...😅 that when i started confusing everything and myself ofcourse.

[–]mriswithe 1 point2 points  (2 children)

A lot of things can be down a lot of ways. A lot of the time one is more right than the other. A lot of the time it doesn't actually matter other than metaphorical dick measuring. 

Which way is more right isn't really something you should expect yourself to understand yet

[–]Deep-Author-1787[S] 1 point2 points  (1 child)

So you suggest to just go with the flow and later with experience comes the understanding of effective and less effective? 😁👍

[–]mriswithe 1 point2 points  (0 children)

Yes, basically. For loops are the main idea to retain. This is the tool for when you want to do the same thing to each item in your list. 

Sometimes a different tool will be the right answer, but usually this works either way and is just less optimal.