Here is a simple example. When NumPy sees operands with different dimensions, it tries to expand (that is, to broadcast) the low-dimensional operand to match the dimensions of the other. By the time you read this article, the prices and the estimates will have changed from what is used here as an example. Faster alternative to for loop in for loop. We are going to use a method to generate Pandas Dataframes filled with random coordinates of 10000, 100000 and 100000 rows to see the efficiency of these methods. If s(i, k) = s(i1, k), the ith item has not been taken. Numpy is a library with efficient data structures designed to hold matrix data. List Comprehensions with Multiple For Loops: You can actually incorporate multiple for loops into a list comprehension to iterate over multiple iterables or to create nested loops. In cases, where that option might need substitution, it might certainly be recommended to use that technique. Conclusions. How do I concatenate two lists in Python? What it is is implementations into Python of popular, and fast, algorithms for dealing with data that can be worked with to get things done using less Python. What were the poems other than those by Donne in the Melford Hall manuscript? Learn to code for free. The list of stocks to buy is rather long (80 of 100 items). Then you can move everything that happens inside the first loop to a function. That will help each iteration run faster, but that's still 6 million items. And the first loop is quite simple, so let's collapse it into listOfLists = [create_list(l1) for l1 in L1]. Your task is to pack the knapsack with the most valuable items. These expressions can then be evaluated over an iterable using the apply() method. The "inner loop" will be executed one time for each iteration of the "outer loop": Example Get your own Python Server Print each adjective for every fruit: adj = ["red", "big", "tasty"] fruits = ["apple", "banana", "cherry"] for x in adj: for y in fruits: print(x, y) Python Glossary Top References Can you make a dict that will have L4 elements for keys and l3 indices for value (you won't to iterate through L3 then), How to speed up nested for loops in Python, docs.python.org/2/extending/extending.html. The itertools module is included in the Python standard library, and is an awesome tool that I would recommend the use of all the time. On the other hand, the size of the problem a hundred million looks indeed intimidating, so, maybe, three minutes are ok? dev. All you need is to shift your mind and look at the things in a different angle. How bad is it? In other words, Python came out 500 times slower than Go. You can obtain it by running the code. Until the knapsacks capacity reaches the weight of the item newly added to the working set (this_weight), we have to ignore this item and set solution values to those of the previous working set.