0

I have the following namedtuple and List:

from collections import namedtuple

firefoxprofile = namedtuple("Profile", ["Name", "Path", "isRelative", "Default"])
jb = firefoxprofile(Name='Jason', Path='Profiles/er5rtak4.Jason', isRelative='1', Default=None)
sr = firefoxprofile(Name='Sarah', Path='Profiles/23mvfqcj.Sarah', isRelative='1', Default=None)

files = ["places.sqlite","key4.db", "logins.json"]

firefoxlisttuple = []
firefoxlisttuple.append(jb)
firefoxlisttuple.append(sr)

I'm using a nested for loop to create the paths to each of the files in the files List.

Example:

for profile in firefoxlisttuple:
    for file in files:
        print("{0}\{1}".format(profile.Path,file))

Output:

Profiles/er5rtak4.Jason/places.sqlite
Profiles/er5rtak4.Jason/key4.db
Profiles/er5rtak4.Jason/logins.json
Profiles/23mvfqcj.Sarah/places.sqlite
Profiles/23mvfqcj.Sarah/key4.db
Profiles/23mvfqcj.Sarah/logins.json

I'm aware that a nested for loop isn't a good idea in terms of preformace. What should I do instead to achieve the same output?

I've looked into these links:

Iterate over two lists with different lengths

Python merging two lists with all possible permutations

but I'm not sure if it's the correct approach. Is permutations the right tool for this task?

  • 3
    1. Any approach is going to have the same asymptotic runtime of the product of the length of both lists. 2. The easiest way to find out which approach is fastest is to time it: https://docs.python.org/2/library/timeit.html – Jonas Jun 22 '18 at 02:48

1 Answers1

3

Even if you get away without 2 explicit for loops any solutions (be it permutations or something else) will result in implicit looping over the two lists under the hood.

Since you are asking about performance of your code there are two angles to it:

  • Big-O - which is about the class of the solution for a significantly large input numbers
  • profiling - here the timeit library is the correct approach to measure it in python (check here for usage).

Your solution is (n*m) in Big-O notation in the function of the length of the two lists (n and m). This does not prevent it from having good performance in reality where both lists are short (in your example they are) and both n and m are small.

sophros
  • 14,672
  • 11
  • 46
  • 75
  • Just for learninig, suppose the list of profiles were bigger, let's say 200+ names, what then should be done? As you stated, at some point I'll be using two for loops under the hood. –  Jun 22 '18 at 12:45
  • @Sveta: Are you really sure it is essential to spend time optimizing a walk through files on disk? I don't think it is a good idea to concentrate on optimization of this code at the level of algorithm or maybe at any level at all. As the saying goes: premature optimization is the root of all evil. I can't readily think of any solution that would not have the complexity of `n*m` and I doubt there exist one for the problem you posed. Have you tried to profile your script? Try doing so and improve the hot-spots. – sophros Jun 23 '18 at 08:00