Python Forum
Help with Dictionaries and List of Lists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with Dictionaries and List of Lists
#1
Hello all!

I have been stuck for far too long on this issue and appreciate any and all help.

I have a global dictionary and I'm trying to add to a key a value which is a list of lists.

So like this:

my_dict = { key1 : [ [name, file, withinOH], [name, file, something], ... ], key2 : [ [...], [...] ] }

I do not know how many lists there will be for a certain key in advance so I need to add them as I parse them.

As I parse my input I will get groups of items which I want to put together into a list. Each of these lists are associated with a single key. BUT they cannot all go into the same list because the groupings of items in each list is important for permissions. Thus the need for list of lists.

My plan is to search my dict for a specific key. Then search all lists inside that dict for a specific value. If found, search that list specifically for another value which should be there, if it's not, then I do not grant permissions.

I think I understand part of the problem with my code because when I create the list I want to place inside my dict of lists, it is a local variable. Somehow that forces it to override even when I use .append to my dict.

This is the code:

PA_dict = {}
def addPA(paLine):

    entries = paLine.split("-")

    for entry in entries:
        parts = entry.split(":")
        n_iter = iter(parts)
        attributesLine = next(n_iter).strip()
        permissionName = next(n_iter).strip() # PR or PW
        attributes = attributesLine.split(";")
        PA_dict[permissionName] = []
        my_list = []

        for attribute in attributes:
            attribute = attribute.strip()
            attribute = attribute.strip("<")
            attribute = attribute.strip(">")
            attributeParts = attribute.split(",")
            name = attributeParts[0].strip()
            value = attributeParts[1].strip()
            # print(name + " " + value + " " + permissionName)
            my_list.append(value)

        print(my_list) #this prints out exactly what I need to go into my PA_dict list of lists
        PA_dict[permissionName] = my_list
augh why isn't it indenting my code
Reply


Messages In This Thread
Help with Dictionaries and List of Lists - by artblinked - Feb-14-2019, 03:23 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Sort a list of dictionaries by the only dictionary key Calab 1 490 Oct-27-2023, 03:03 PM
Last Post: buran
  Access list of dictionaries britesc 4 1,072 Jul-26-2023, 05:00 AM
Last Post: Pedroski55
  List all possibilities of a nested-list by flattened lists sparkt 1 919 Feb-23-2023, 02:21 PM
Last Post: sparkt
  user input values into list of lists tauros73 3 1,071 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  returning a List of Lists nafshar 3 1,064 Oct-28-2022, 06:28 PM
Last Post: deanhystad
  Creating list of lists, with objects from lists sgrinderud 7 1,634 Oct-01-2022, 07:15 PM
Last Post: Skaperen
  How to efficiently average same entries of lists in a list xquad 5 2,121 Dec-17-2021, 04:44 PM
Last Post: xquad
  sorting a list of lists by an element leapcfm 3 1,866 Sep-10-2021, 03:33 PM
Last Post: leapcfm
  behavior list of lists roym 5 2,092 Jul-04-2021, 04:43 PM
Last Post: roym
  List of lists - merge sublists with common elements medatib531 1 3,402 May-09-2021, 07:49 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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