Python Forum
Creating directories from two lists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating directories from two lists
#1
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?
Reply
#2
In this case, I think you can use some syntax sugars to "hide" (but not avoid) the for loops.
Maybe using product you can get a better performance (test it for large amount of entries).
myrange = [
    [x for x in firefoxlisttuple],
    [y for y in files],
]
for profile, file in itertools.product(*myrange):
    print("{0}\{1}".format(profile.Path, file))
And if your files array will be the same all the time, you can use direct assignment:
for profile in firefoxlisttuple:
    print("{0}\{1}".format(profile.Path, files[0]))
    print("{0}\{1}".format(profile.Path, files[1]))
    print("{0}\{1}".format(profile.Path, files[2]))
You'll have to analyse what are the lengths of the arrays and check the performance.
Reply
#3
(Jun-22-2018, 02:51 AM)QueenSveta Wrote: ......
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?

Nothing wrong with nested loops. Of course, if you want to create a list - or list of lists - then list comprehension will give a better performance (not that performance is an issue in your case).

You could have used itertools.product - as advised by @gontajones,
for profile, file in itertools.product(firefoxlisttuple, files):
    print("{0}\{1}".format(profile.Path,file))
but performance-wise it may be even worth (who measures performance when printing Tongue )

Questioning yourself is an admirable quality, but in that case you are falling into the trap of "Pythonic" objection to loops - which is a false claim, the choice between loop and comprehension is context-specific.

And remember,
Premature optimization is the root of all evil
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Organization of project directories wotoko 3 430 Mar-02-2024, 03:34 PM
Last Post: Larz60+
  Listing directories (as a text file) kiwi99 1 840 Feb-17-2023, 12:58 PM
Last Post: Larz60+
  Find duplicate files in multiple directories Pavel_47 9 3,111 Dec-27-2022, 04:47 PM
Last Post: deanhystad
  rename same file names in different directories elnk 0 712 Nov-04-2022, 05:23 PM
Last Post: elnk
  Creating list of lists, with objects from lists sgrinderud 7 1,638 Oct-01-2022, 07:15 PM
Last Post: Skaperen
  I need to copy all the directories that do not match the pattern tester_V 7 2,441 Feb-04-2022, 06:26 PM
Last Post: tester_V
  Functions to consider for file renaming and moving around directories cubangt 2 1,756 Jan-07-2022, 02:16 PM
Last Post: cubangt
  Creating list of lists from generator object t4keheart 1 2,206 Nov-13-2020, 04:59 AM
Last Post: perfringo
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,376 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  Python create directories within directories mcesmcsc 2 2,219 Dec-17-2019, 12:32 PM
Last Post: mcesmcsc

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020