I haveing trouble understanding python dictonary
let say I have a dictornary called data that looks like this:
{ "computers": [
{"Netbios_Name0": "apple1", "User_Domain0": "paradise", "User_Name0": "adam", "SMS_Installed_Sites0": "heaven", "Client_Version0": "5.00.9058.1018"},
{"Netbios_Name0": "apple2", "User_Domain0": "paradise", "User_Name0": "lilith", "SMS_Installed_Sites0": "heaven", "Client_Version0": "5.00.9040.1044"},
{"Netbios_Name0": "apple3", "User_Domain0": "paradise", "User_Name0": "eve", "SMS_Installed_Sites0": "heaven", "Client_Version0": "5.00.9068.1026"}
]}
and I want to remove apple2 so my end results looks like this:
{ "computers": [
{"Netbios_Name0": "apple1", "User_Domain0": "paradise", "User_Name0": "adam", "SMS_Installed_Sites0": "heaven", "Client_Version0": "5.00.9058.1018"},
{"Netbios_Name0": "apple3", "User_Domain0": "paradise", "User_Name0": "eve", "SMS_Installed_Sites0": "heaven", "Client_Version0": "5.00.9068.1026"}
]}
I suspect I have to do something like this:
for item in data['computers']:
if "apple2" in item['Netbios_Name0']:
item.pop() # or del data[item]
But I can't get it to work.
CodePudding user response:
You can use remove()
:
data = { "computers": [
{"Netbios_Name0": "apple1", "User_Domain0": "paradise", "User_Name0": "adam", "SMS_Installed_Sites0": "heaven", "Client_Version0": "5.00.9058.1018"},
{"Netbios_Name0": "apple2", "User_Domain0": "paradise", "User_Name0": "lilith", "SMS_Installed_Sites0": "heaven", "Client_Version0": "5.00.9040.1044"},
{"Netbios_Name0": "apple3", "User_Domain0": "paradise", "User_Name0": "eve", "SMS_Installed_Sites0": "heaven", "Client_Version0": "5.00.9068.1026"}
]}
for item in list(data['computers']):
if 'apple2' in item['Netbios_Name0']:
data['computers'].remove(item)
data
Output:
{'computers': [{'Netbios_Name0': 'apple1',
'User_Domain0': 'paradise',
'User_Name0': 'adam',
'SMS_Installed_Sites0': 'heaven',
'Client_Version0': '5.00.9058.1018'},
{'Netbios_Name0': 'apple3',
'User_Domain0': 'paradise',
'User_Name0': 'eve',
'SMS_Installed_Sites0': 'heaven',
'Client_Version0': '5.00.9068.1026'}]}
CodePudding user response:
You are iterating a list - you can pop from a list by index.
Never modify a lists length while while iterating: How to remove items from a list while iterating?
One way would be to store the indexes to be removed and remove them later:
data = { "computers": [
{"Netbios_Name0": "apple1", "User_Domain0": "paradise", "User_Name0": "adam",
"SMS_Installed_Sites0": "heaven", "Client_Version0": "5.00.9058.1018"},
{"Netbios_Name0": "apple2", "User_Domain0": "paradise", "User_Name0": "lilith",
"SMS_Installed_Sites0": "heaven", "Client_Version0": "5.00.9040.1044"},
{"Netbios_Name0": "apple3", "User_Domain0": "paradise", "User_Name0": "eve",
"SMS_Installed_Sites0": "heaven", "Client_Version0": "5.00.9068.1026"}
]}
to_delete = []
for idx, inner_dic in enumerate(data['computers']):
if "apple2" in inner_dic['Netbios_Name0']:
to_delete.append(idx)
# remove biggest to lowest indexes - removing does not influence the order
for idx in to_delete[::-1]:
data['computers'].pop(idx)
print(data)
Output:
{'computers':
[{'Netbios_Name0': 'apple1', 'User_Domain0': 'paradise', 'User_Name0': 'adam',
'SMS_Installed_Sites0': 'heaven', 'Client_Version0': '5.00.9058.1018'},
{'Netbios_Name0': 'apple3', 'User_Domain0': 'paradise', 'User_Name0': 'eve',
'SMS_Installed_Sites0': 'heaven', 'Client_Version0': '5.00.9068.1026'}]
}
CodePudding user response:
Python’s built-in enumerate function allows us to loop over a list and retrieve both the index and the value of each item in the list:
data = { "computers": [
{"Netbios_Name0": "apple1", "User_Domain0": "paradise", "User_Name0": "adam", "SMS_Installed_Sites0": "heaven", "Client_Version0": "5.00.9058.1018"},
{"Netbios_Name0": "apple2", "User_Domain0": "paradise", "User_Name0": "lilith", "SMS_Installed_Sites0": "heaven", "Client_Version0": "5.00.9040.1044"},
{"Netbios_Name0": "apple3", "User_Domain0": "paradise", "User_Name0": "eve", "SMS_Installed_Sites0": "heaven", "Client_Version0": "5.00.9068.1026"}
]}
# Here 0 in 2nd argument is the starting number for num variable, can be used 0 to indicate as index
for num, computer in enumerate(data["computers"], 0):
if computer["Netbios_Name0"] == "apple2":
del data["computers"][num]
print(data)
This should work.
You can find more here: https://realpython.com/iterate-through-dictionary-python/