return in for loop or outside loop

Now someone told me that this is not very good programming because I use the return statement inside a loop and this would cause garbage collection to malfunction. That’s incorrect, and suggests you should treat other advice from that person with a degree of skepticism. The mantra of “only have one return statement” (or more … Read more

random iteration in Python

You can use random.shuffle() to, well, shuffle a list: import random r = list(range(1000)) random.shuffle(r) for i in r: # do something with i By the way, in many cases where you’d use a for loop over a range of integers in other programming languages, you can directly describe the “thing” you want to iterate … Read more

Range-for-loops and std::vector

Because std::vector<bool> is not a container ! std::vector<T>‘s iterators usually dereference to a T&, which you can bind to your own auto&. std::vector<bool>, however, packs its bools together inside integers, so you need a proxy to do the bit-masking when accessing them. Thus, its iterators return a Proxy. And since the returned Proxy is an … Read more

How do I efficiently find which elements of a list are in another list?

I thought it would be useful to actually time some of the solutions presented here on a larger sample input. For this input and on my machine, I find Cardstdani’s approach to be the fastest, followed by the numpy isin() approach. Setup 1 import random list_1 = [random.randint(1, 10_000) for i in range(100_000)] list_2 = … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)