I've been trying to modify a function and every attempt seem to screw up the plist.

def removeItem(pl, item_name):
    for dock_item in pl['persistent-apps']:
        if dock_item['tile-data']['file-label'] == item_name:
            verboseOutput('found', item_name)
            pl['persistent-apps'].remove(dock_item)
            return True
    for dock_item in pl['persistent-others']:
        if dock_item['tile-data']['file-label'] == item_name:
            verboseOutput('found', item_name)
            pl['persistent-others'].remove(dock_item)
            return True
    return False

This scans through a dictionary array and looks for a match to a label name. If found it deletes the array element. I need to change this so I can supply an index and it will delete that element. I'm a total noob in python and I can't seen to find the right solution in the web docs I've read. My attempted solutions also added an additional parameter for 'persistent-apps' or 'persistent-others', so the above function is reduced to a single for loop. I'm actually trying to do this without the for loop at all. Can someone show me how to specify dock_item as it's position in the array?

This is how I plan to call the function;

def removeIndexItem(pl, pl_part, item_index):

Thanks in advance...

Ok, yes I'm a noob and this is a really simple thing... but can someone please point me in the right direction. I'm kinda stuck until I resolve this...

This is how I plan to call the function;

def removeIndexItem(pl, pl_part, item_index):

Quite impossible to say without concrete example of content of parameters. What kind arrays are you using? Numpy? Array module? Why you are not using normal lists?
Maybe you want to just do

def removeIndexItem(pl, pl_part, item_index):
     try:
          del pl[pl_part, item_index]
     except:
          return False
     else:
          return True

l

Hi tonyjv... thanks for the reply.

I'm sure I haven't provided any where near enough information for you all to help me on this. Sorry!

tony, I tried your submittal, but couldn't make things work. Again, I'm sure it's my complete lack of knowledge of Python that can be blamed.

I was hoping for a replacement for the for loop that would just assign dock_item from pl. I'm pretty sure I need to maintain the use of remove.

I did just think of a very non-elegant way of doing this and despite my experience and understanding of Python, I realize there must be a better way to do this. On the other hand this does work...

def removeIndexItem(pl, sec, idx):
    t = 0
    for dock_item in pl[sec]:
        t = t + 1
        if t == int(idx):
            pl[sec].remove(dock_item)
            return True
    return False

it seems walking a list just to find it's index is slow and clumsy. Anyone have a better solution?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.