Reputation: 99
I am new to python and learning about the lists. I am trying to create a function that will remove a specific value from the list. I know there is a .remove()
function to remove elements but it doesn't work on the nested list.
def remove(list, x):
if x in list:
return list.remove(x)
mylist = [[1, 2, 3, 2, 4, 8, 2],2]
remove(mylist, 2)
print(mylist)
The result I'd like to get:
[1, 3, 4, 8]
Appreciate your help in advance
Upvotes: 1
Views: 3491
Reputation: 1921
It is not clear from your example whether you really want a flattened list as output or a you'd like to still keep the structure of your list. And also how many levels of nesting you will have. So I have written a recursive function which will remove item from a deeply nested list without flattening as well as flattening the output.
To make my solutions clear, I have modified your input list to the following.
mylist = [[1, 2, 3, 2, 4, 8, 2],2,3,34,2,[[3,4],3,[1,2]]]
Function
def rem_list_item(l, remval, flatten=False):
"""Recursive function to remove an item from a nested list"""
flatlist = []
for item in l:
if isinstance(item, list):
# Calling the function itself recursively if the item
# is a list.
if flatten:
# += will make sure the list is appended without nesting.
flatlist+=rem_list_item(item, remval, flatten=True)
else:
# appended the sublist so that nesting is preserved
flatlist.append(rem_list_item(item, remval, flatten=False))
else:
# If the item is not a list, just append it to the return list
if item != remval:
flatlist.append(item)
return flatlist
Without flattening
rem_list_item(mylist, 2, flatten=False)
Out: [[1, 3, 4, 8], 3, 34, [[3, 4], 3, [1]]]
With flattening
rem_list_item(mylist, 2, flatten=True)
Out: [1, 3, 4, 8, 3, 34, 3, 4, 3, 1]
Upvotes: 3
Reputation: 4663
I customize your code here you can see an online version https://onlinegdb.com/Sk2oA9KGV
Continue and read the description if you don't understand
def remove(list, x):
if x in list:
return list.remove(x)
mylist = [[1, 2, 3, 2, 4, 8, 2],2]
for i in range(len(mylist)):
if isinstance(mylist[i], list) == True :
mylist[i] = list(set(mylist[i]))
mylist[i].remove(2)
mylist[i].sort()
else :
if mylist[i] == 2 :
del mylist[i]
print(mylist)
print()
the final result is like this
print(mylist)
[[1, 3, 4, 8]]
print(mylist[0])
[1, 3, 4, 8]
Complete description
First of all, you need to use for loop to separate the arrays in a multidimensional array
for i in range(len(mylist)):
Then you should check that the current variable that you have is array
or not
if isinstance(mylist[i], list) == True :
If the condition being True then you continue
To remove duplicate items here 2
you need to this line
mylist[i] = list(set(mylist[i]))
And then run your remove funtion
mylist[i].remove(2)
After this, your result will be like this [8, 1, 3, 4]
Then you should sort your array by this code
mylist[i].sort()
And one more thing that if your variable was not arrayed you should check that if it is matched with your item that you want delete it or not if match then delete that
if mylist[i] == 2 :
del mylist[i]
Upvotes: 4