Delete Matching Elements in two Arrays - Python [duplicate] - python

This question already has answers here:
How to remove items from a list while iterating?
(25 answers)
Closed 3 years ago.
def array_diff(a, b):
b_index=0
while b_index < len(b):
for x in range(len(a)):
if b[b_index]=a[x]:
del a[x]
b_index+=1
print(a)
array_diff([1,2,3,4,5,6,6,7],[1,3,6])
causes a runtime error because i am mutating the container while still iterating over it
what exactly will be the best practice to delete the matching items and return the list without the items
Initial List [1,2,3,4,5,6,6,7]
Final List [2,4,5,7]

Here is how you can return the result in a new list:
elements = [1, 2, 3, 4, 5, 6, 6, 7]
exclude = [1, 3, 6]
result = [e for e in elements if e not in exclude]
# result == [2, 4, 5, 7]

Related

iterating list through a function [duplicate]

This question already has answers here:
Accessing the index in 'for' loops
(26 answers)
Closed 8 months ago.
I have a function that takes a list as parameter (have used a very simple function here to focus on the point of my question).
def some_lst_func(lst):
foo = len(lst)
return foo
lst1= [1, 6, 6, 7, 7, 5]
print(some_lst_func(lst1))
For the whole list this works fine, but I want to incrementally pass the list ([1], [1,6].....) to the function and record each output (i.e. increasing length of list).
Below is what I have tried but is clearly not correct, and not sure what the output is that I am getting, what I would expect is
1
2
3...
for num in lst1:
print(some_lst_func(lst1[:num]))
You need to loop over indices, not items:
def some_lst_func(lst):
return len(lst)
lst1= [1, 6, 6, 7, 7, 5]
for num in range(1, len(lst1) + 1):
print(some_lst_func(lst1[:num]))
# 1
# 2
# 3
# 4
# 5
# 6
# alternatively, using enumerate:
for num, _ in enumerate(lst1, start=1):
print(some_lst_func(lst1[:num]))

How do I move items from one list to another? [duplicate]

This question already has answers here:
How to remove items from a list while iterating?
(25 answers)
Closed 10 months ago.
I am trying to remove all the items from list2 into list1 like this:
l1 = [1, 2, 3]
l2 = [4, 5, 6]
for item in l2:
l1.append(item)
l2.remove(item)
the problem is:
#l1 now returns [1, 2, 3, 4, 6]
#l2 now returns [5]
I want it so that:
#l1 returns [1, 2, 3, 4, 5, 6]
#l2 returns []
Why does this happen? And what is the conventional way to achieve what I'm trying to do, in Python?
Your code doesn't work because you are removing items while iterating.
For your problem - moving all items from one list to another list - the best solution is to use built-in methods provided by lists (the latter since Python 3.3):
l1.extend(l2)
l2.clear()
you can replace l2.clear() with l2 = [] if there are no other references to l2

python function only returns the first element of the first sublist [duplicate]

This question already has answers here:
What is the purpose of the return statement? How is it different from printing?
(15 answers)
How can I use `return` to get back multiple values from a loop? Can I put them in a list?
(2 answers)
Closed 12 months ago.
I want to print all elements of all sublists which I am able to do with the following loop
sublists=[[1, 2, 3], [4, 5, 6], [7, 7, 7]]
for i in sublists:
for j in i:
print(j)
but if I use this function, it only prints the first item of the first sublist.
Why is that? How do I make this function work ?
def sl_check(sl):
for i in sl:
for j in i:
return j
print(sl_check(sublists))
The return breaks out of the loop, so you'll need to collect all the elements in a new list and then return everything outside the loop:
def sl_check(sl):
all_elements = []
for i in sl:
for j in i:
all_elements.append(j)
return all_elements
sublists=[[1, 2, 3], [4, 5, 6], [7, 7, 7]]
print(sl_check(sublists))

Issue removing duplicates from list [duplicate]

This question already has answers here:
Strange result when removing item from a list while iterating over it
(8 answers)
Closed 3 years ago.
I am trying to remove duplicate elements from a list. The code is removing the wrong elements.
def simplify(A):
for i in A:
for j in A:
if i == j:
A.remove(j)
return A
Input A:
A = [1, 2, 5, 8, 12, 5]
Output:
A = [2, 8]
Try this.
def RemoveDuplicates(list_):
return list(set(list_))
You can call this function by passing your list and you get back listwithout duplicates.

Remove duplicate without modifying a list while iterating through the same list (python) [duplicate]

This question already has answers here:
How do I remove duplicates from a list, while preserving order?
(31 answers)
Closed 8 years ago.
I have tried
def remove_extra(lst):
return print(sorted(set(lst), key = lst.index))
def remove_extra(lst):
b = []
for x in lst:
if x not in b:
b.append(x)
print(b)
All sort of removing duplicates codes however I don't know one that
give me True when result1 is lst1 where result1 = remove_extra(lst1)
data = [1, 2, 3, 4, 4, 2, 1]
def remove_extra(lst):
seen, result = set(), []
for item in lst:
if item not in seen:
result.append(item)
seen.add(item)
lst[:] = result
return lst
print data is remove_extra(data)
# True
print data
[1, 2, 3, 4]

Categories