Iterate through a C++ Vector using a ‘for’ loop

Is there any reason I don’t see this in C++? Is it bad practice? No. It is not a bad practice, but the following approach renders your code certain flexibility. Usually, pre-C++11 the code for iterating over container elements uses iterators, something like: This is because it makes the code more flexible. All standard library containers … Read more

Why does python use ‘else’ after for and while loops?

It’s a strange construct even to seasoned Python coders. When used in conjunction with for-loops it basically means “find some item in the iterable, else if none was found do …”. As in: But anytime you see this construct, a better alternative is to either encapsulate the search in a function: Or use a list … Read more

TypeError: ‘list’ object cannot be interpreted as an integer

Error messages usually mean precisely what they say. So they must be read very carefully. When you do that, you’ll see that this one is not actually complaining, as you seem to have assumed, about what sort of object your list contains, but rather about what sort of object it is. It’s not saying it wants your list to … Read more