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
#2
There are ways in which you can do what you describe, but it's a bad idea (although better than your current pickle awfulness).
Rather than creating variable names dynamically, you should just be adding your lists to a dictionary.
Reply
#3
How would that help? I would still have to go through the dict to find all the entries that contain a particular value. I could just iterate over the original list with it's 13,000 items. The point of making new lists is to shorten the search time.
Reply
#4
There's no big difference between having a list named a and having a list named lists['a']
Reply
#5
I have a list that contains 13,000 entries. I want to create several new lists from it that contain only the proportion of the first list that contains specific data. When a search is performed it would only have to examine the smaller list containing the main criterion. So I would be checking a list of say 1000 items instead of 13,000.
Reply
#6
I see you don't understand what I'm suggesting, maybe example code would help?
>>> from collections import defaultdict
>>> lists = defaultdict(list)
>>> main_list = range(10000)
>>>
>>> for element in main_list:
...     if element % 50 == 0:
...         lists['a'].append(element)
...     if element % 1000 == 1:
...         lists['b'].append(element)
...
>>> lists['a']
[0, 50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600, 650, 700, 750, 800, 850, 900, 950, 1000, 1050, 1100, 1150, 1200, 1250, 1300, 1350, 1400, 1450, 1500, 1550, 1600, 1650, 1700, 1750, 1800, 1850, 1900, 1950, 2000, 2050, 2100, 2150, 2200, 2250, 2300, 2350, 2400, 2450, 2500, 2550, 2600, 2650, 2700, 2750, 2800, 2850, 2900, 2950, 3000, 3050, 3100, 3150, 3200, 3250, 3300, 3350, 3400, 3450, 3500, 3550, 3600, 3650, 3700, 3750, 3800, 3850, 3900, 3950, 4000, 4050, 4100, 4150, 4200, 4250, 4300, 4350, 4400, 4450, 4500, 4550, 4600, 4650, 4700, 4750, 4800, 4850, 4900, 4950, 5000, 5050, 5100, 5150, 5200, 5250, 5300, 5350, 5400, 5450, 5500, 5550, 5600, 5650, 5700, 5750, 5800, 5850, 5900, 5950, 6000, 6050, 6100, 6150, 6200, 6250, 6300, 6350, 6400, 6450, 6500, 6550, 6600, 6650, 6700, 6750, 6800, 6850, 6900, 6950, 7000, 7050, 7100, 7150, 7200, 7250, 7300, 7350, 7400, 7450, 7500, 7550, 7600, 7650, 7700, 7750, 7800, 7850, 7900, 7950, 8000, 8050, 8100, 8150, 8200, 8250, 8300, 8350, 8400, 8450, 8500, 8550, 8600, 8650, 8700, 8750, 8800, 8850, 8900, 8950, 9000, 9050, 9100, 9150, 9200, 9250, 9300, 9350, 9400, 9450, 9500, 9550, 9600, 9650, 9700, 9750, 9800, 9850, 9900, 9950]
>>> lists['b']
[1, 1001, 2001, 3001, 4001, 5001, 6001, 7001, 8001, 9001]
Reply
#7
Oh, now I get it, sorry. That'll work. I'll have a play. Thanks.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Delete strings from a list to create a new only number list Dvdscot 8 1,511 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,508 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  how to easily create a list of already existing item CompleteNewb 15 3,527 Jan-06-2022, 12:48 AM
Last Post: CompleteNewb
  Create SQLite columns from a list or tuple? snakes 6 8,662 May-04-2021, 12:06 PM
Last Post: snakes
  Create variable and list dynamically quest_ 12 4,388 Jan-26-2021, 07:14 PM
Last Post: quest_
  How to create a linked list and call it? loves 12 4,490 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,032 Aug-14-2020, 12:37 PM
Last Post: Yoriz
  How to define a function to create a resorted list? sparkt 6 2,810 Aug-08-2020, 04:10 PM
Last Post: sparkt
  Create a program that PING a list of IPs skaailet 7 6,326 Mar-26-2020, 10:46 PM
Last Post: snippsat
  Create a list of co-ordinates from Mouse press events Jagruthi 0 1,474 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