2

I am trying to remove items from slices(list) whose substring is a part of pic(string) value from status2. I have tried various ways but there is always some issue such as sometimes it doesn't iterate over every item, sometimes it returns the exact same list.

slices = ['face_06_01.png', 'face_B45_06_01.png', 'face_gh_06_01.png', 'face_HJ_06_01.png']
status2 = [{'key': 1, 'pic': 'face_01_01.png', 'status': 2}, {'key': 2, 'pic': 'face__B45_02_01.png', 'status': 2}, {'key': 4, 'pic': 'face_gh_04_01.png', 'status': 2}]

for part in slices:
    pic_name = part[:-10]
    for status2_pic in status2:
        if pic_name in status2_pic['pic']:
            slices.remove(part)
            break

Output I get:

slices = ['face_B45_06_01.png', 'face_HJ_06_01.png']

Output I want according to the sample data:

slices = ['face_HJ_06_01.png']

This is where I am testing the code for now: enter image description here

Braiam
  • 1
  • 11
  • 47
  • 78
ali baig
  • 71
  • 5
  • CAn you provide an example where this does *not* work? – CutePoison Jul 30 '21 at 07:44
  • Your code gives the output you need. What is the issue ? – Ram Jul 30 '21 at 07:46
  • Does this answer your question? [How to remove items from a list while iterating?](https://stackoverflow.com/questions/1207406/how-to-remove-items-from-a-list-while-iterating) – Iguananaut Jul 30 '21 at 07:47
  • I marked this as a duplicate, because from looking at your code I believe you're running into the common problem in Python of removing items from a list while iterating over it, which can cause exactly the kinds of problems you're describing. One quick workaround is to duplicate the list when iterating over it like `for part in slices[:]:` – Iguananaut Jul 30 '21 at 07:48
  • I have updated the code @Ram – ali baig Jul 30 '21 at 07:57
  • Please see the example in the code. I have updated it. @CutePoison – ali baig Jul 30 '21 at 07:58
  • Tried it but not getting the right output. @Iguananaut – ali baig Jul 30 '21 at 07:58
  • 1
    Note that you have a double under-score in `key:2` `face__B45` – CutePoison Jul 30 '21 at 08:06
  • Could you explain the output you want. How are you getting that ? @alibaig – Ram Jul 30 '21 at 08:23
  • Fixed the type but still the same result @CutePoison – ali baig Jul 30 '21 at 09:10
  • I am trying to remove items from slices(list) whose substring is a part of pic(string) value from status2. I have mentioned the output I want in the question. @Ram – ali baig Jul 30 '21 at 09:11
  • I mean add an example in your question and explain us how you are getting that output you wanted. So ```f``` is a substring in all the items of ```slices``` and all values of ```pic```. Does that mean you have to remove everything from ```slices``` ? – Ram Jul 30 '21 at 09:15
  • The list of Slices contains file names as I have provided the sample list in the question. The other variable is status2 which has a pic(string) in it. I want to match the start of pic(String) such as in the case of face_BHd_01_01.png; it would be face_BHD, in case of face_01_01.png; it would be face. Now I want to match face_BHD and face with each value of slices if this substring is a part of slices item values, then that item should be removed from the slices. – ali baig Jul 30 '21 at 09:24
  • Ok. So for ```face_gh_06_01.png``` the slice is ```face_gh```; ```face_HJ_06_01.png``` the slice is ```face_HJ```. Am I right ? – Ram Jul 30 '21 at 09:30
  • Yes, exactly @Ram – ali baig Jul 30 '21 at 09:32

2 Answers2

2

This should work. You don't have to remove the items one by one. List comprehensions are your friend :)

slices = [slice for slice in slices if slice[:-10] not in str(list(map(lambda x: x['pic'], status2)))]
alparslan mimaroğlu
  • 1,450
  • 1
  • 10
  • 20
2

You can try something like this using List Comprehension.

  • I am using .startswith() to specifically filter out results that start with the substring.
  • If you just want that substring to exists irrespective of position. Use in.

    if s in i['pic']:

slices = ['face_06_01.png', 'face_B45_06_01.png', 'face_gh_06_01.png', 'face_HJ_06_01.png']
status2 = [{'key': 1, 'pic': 'face_01_01.png', 'status': 2}, {'key': 2, 'pic': 'face__B45_02_01.png', 'status': 2}, {'key': 4, 'pic': 'face_gh_04_01.png', 'status': 2}]

def check(s):
    for i in status2:
        if i['pic'].startswith(s):
            return True
    return False

slices = [x for x in slices if not check(x[:-9])]

print(slices)
['face_B45_06_01.png', 'face_HJ_06_01.png']
Ram
  • 4,724
  • 2
  • 14
  • 22
  • `slices = ['face_HJ_06_01.png']` It should return this but thank you for the help. – ali baig Jul 30 '21 at 09:55
  • It's working fine. there's was a typo in face__B45 which should be face_B45 and then it would display the correct result – ali baig Jul 30 '21 at 09:57