简体   繁体   中英

How to remove items from a list on the SAME INDEX of duplicates i removed from another list

I have two lists of items, one for avatar names, and one for avatar pictures:

name_list = ['name1', 'name2', 'name2', 'name3', 'name4', 'name5', 'name5']
picture_list = ['pic1', 'pic2', 'pic3', 'pic4', 'pic5', 'pic6', 'pic7']

I need each item from the name_list to always stay on the same index it shares with the item in the picture_list. So that when I remove the duplicates from the name_list, the items in the picture_list will be removed accordingly. So that the result is:

name_list = ['name1', 'name2', 'name3', 'name4', 'name5']
picture_list = ['pic1', 'pic2', 'pic4', 'pic5', 'pic6']

(It's important to mention that the numbers of the picture and the name don't have to be the same. only that they'll stay on the same index they shared before)

How may I do so in code?

You might consider using nested lists. Consider the following:

item_lists = [
    ['name1', 'pic1'],
    ['name2', 'pic2'],
    # etc.
]

del item_lists[1]

This way you always keep the name and pictures together, and delete them simultaneously.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM