Python Forum
Create a new list dynamically
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Create a new list dynamically
#1
I have a list of dicts. One of the values in the dict contains several words semi-colon separated which I would like to be the names of dynamically created lists. I can iterate over the master list and separate the required list names but cannot figure out how to create and name the lists. e.g.

dict1 contains a,b,c I need to create lists a b and c and copy dict1 into each.

dict2 contains a,c,g I need to append dict2 to lists a and c, and create list g then copy dict2 into it.

At the moment I achieve this by creating pickled items on disc, as creating named files dynamically is a breeze. But as the master list grows the process slows...a lot!

I'd appreciate a pointer or two, thanks.
This is the disc/pickle version:

def CreateDict():

    with open(music, "rb") as f:
        try:
            while True:
                disc_data = pickle.load(f)
        except EOFError:
            pass
            
    for i in disc_data:
        category = (i['categories']).split(';')
        for cat in category:
            cat_data=[]
            cat = cat.lstrip()
            if cat =="":cat = "No category"

            cat_dir = db_dir+cat+'.db'
            if not os.path.isfile(cat_dir):open(cat_dir, 'w').close()
            
            with open(cat_dir, "rb") as h:
                try:
                    while True:
                        cat_data = pickle.load(h)
                except EOFError:
                    pass

            cat_data.append(i)
            output1 = open((cat_dir), 'wb')
            pickle.dump(cat_data, output1)
            output1.close()
Reply


Messages In This Thread
Create a new list dynamically - by floatingshed - Nov-19-2017, 08:23 AM
RE: Create a new list dynamically - by stranac - Nov-19-2017, 09:49 AM
RE: Create a new list dynamically - by floatingshed - Nov-19-2017, 07:05 PM
RE: Create a new list dynamically - by stranac - Nov-19-2017, 07:29 PM
RE: Create a new list dynamically - by floatingshed - Nov-19-2017, 09:48 PM
RE: Create a new list dynamically - by stranac - Nov-20-2017, 12:37 PM
RE: Create a new list dynamically - by floatingshed - Nov-20-2017, 01:25 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Delete strings from a list to create a new only number list Dvdscot 8 1,584 May-01-2023, 09:06 PM
Last Post: deanhystad
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 1,568 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  how to easily create a list of already existing item CompleteNewb 15 3,634 Jan-06-2022, 12:48 AM
Last Post: CompleteNewb
  Create SQLite columns from a list or tuple? snakes 6 8,806 May-04-2021, 12:06 PM
Last Post: snakes
  Create variable and list dynamically quest_ 12 4,481 Jan-26-2021, 07:14 PM
Last Post: quest_
  How to create a linked list and call it? loves 12 4,628 Nov-22-2020, 03:50 PM
Last Post: loves
  How to create and define in one line a 2D list of class objects in Python T2ioTD 1 2,067 Aug-14-2020, 12:37 PM
Last Post: Yoriz
  How to define a function to create a resorted list? sparkt 6 2,862 Aug-08-2020, 04:10 PM
Last Post: sparkt
  Create a program that PING a list of IPs skaailet 7 6,435 Mar-26-2020, 10:46 PM
Last Post: snippsat
  Create a list of co-ordinates from Mouse press events Jagruthi 0 1,494 Mar-12-2020, 08:46 AM
Last Post: Jagruthi

Forum Jump:

User Panel Messages

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