About Deleting a List Included in a Dictionary

Asked 2 years ago, Updated 2 years ago, 32 views

list=[]
dict={}
key=sq{}.format(moji)
list.append(su)
dict[key] = list

as
{'Ah':['10', '20', 'i':['11', '21']}

I included a list in the dictionary so that
I would like to delete the key 'i' and ['11', '21'] in the list.

dict[key].pop()
list.pop()

Then 'i' will be deleted, but
If you create 'i' with the same key, it becomes : ['11', '31' and only the last content is deleted

list.Clear()

will delete everything.

 'i': ['11', '21']

Could you tell me how to delete only completely?
Thank you for your cooperation.

python python3

2022-09-29 20:26

1 Answers

list or dict exist embedded, so it's better to use a different name


Mapping type---dict as described in
If you want to delete the key, do the following:

< preleukemia > < code > = { ' cross, ' : [ ' 10 ', 20 ' ] , ' a title of ' : [ " 11 ", ' 21 ' ] } k = 'い' del d[k] print(d)

If you are unsure if a key exists, you can check it as follows:

 if 'i' ind:
    deld ['I']

Add

If you want to manage each list of keys, you can do the following:

lst=d['Ah']
lst.append('300')

# or
Forkind:
    lst = d[k]
    lst.pop()
# Or maybe
fork, lst ind.items():
    print(f'key: {k}, list: {lst}')
    lst.append('A')


2022-09-29 20:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.