+ 2

Need help with my python program

I have implemented a program but I am not able to use the pop() method with my list. Please look at the code for better explanation. Thank you! https://code.sololearn.com/cSd4arJZp3Rd/?ref=app

3rd Jun 2021, 8:41 AM
Lambda
Lambda - avatar
20 Réponses
+ 7
# this one should be at least as much efficient and more 'pythonic' / short: def array_diff(a, b): return list(filter(lambda v: v not in b,a)) x = [1, 2, 2, 3] z = [2] print(array_diff(x, z))
3rd Jun 2021, 9:10 AM
visph
visph - avatar
+ 5
Bahha🐧 , i have some doubts with your code, but maybe you can explain it. your code produces this output: [1, 2, 3] when giving input: x = [1, 2, 2, 2, 3] z = [2] but should be: [1, 3]
3rd Jun 2021, 9:24 AM
Lothar
Lothar - avatar
+ 5
Bahha🐧 , as visph already mentioned, using set for this task is not recommended as set does not allow duplicates. an other issue by using sets is, that it not may keep the initial order as described by the op. using: x = [1, 2, 2, 3, 0] z = [2] should return [1, 3, 0] but it resulted to: [0, 1, 3]
3rd Jun 2021, 11:24 AM
Lothar
Lothar - avatar
+ 3
rather use: def array_diff(a, b): if len(a) == 0: return [] elif len(b) == 0: return a else: for v in b: while v in a: a.remove(v) return a
3rd Jun 2021, 8:56 AM
visph
visph - avatar
+ 2
lst1 = [1,2,3,4,5] lst2 = [3,4] it should return [1,2,5] ?
3rd Jun 2021, 8:48 AM
lisa
+ 2
✳AsterisK✳ Thank you for the reply. I have tried that on line 27 after the "if b[i]== a[j]" condition it gives me error says, " int object is not subscriptable"
3rd Jun 2021, 8:58 AM
Lambda
Lambda - avatar
+ 2
much efficient solution would be to build a new list and return it, until you want to modify the list in place (in wich case, that's not required to return it)
3rd Jun 2021, 9:01 AM
visph
visph - avatar
+ 2
Bahha🐧 Thank you for the explanation now I got it.
3rd Jun 2021, 9:02 AM
Lambda
Lambda - avatar
+ 2
def array_diff(a,b): for i in b: while i in a: try: a.remove(i) except ValueError: return[] return a a=[1,3,3,4] b=[1,3] print(array_diff(a,b)) #Alternate solution by me
3rd Jun 2021, 9:31 AM
Lambda
Lambda - avatar
+ 2
Bahha🐧 in my last post, I talked about your 'set' solution wich fail for: x = [1,1,2,2,3] z = [2] as result would be: [1,3] but expected is [1,1,3] ^^
3rd Jun 2021, 9:37 AM
visph
visph - avatar
+ 2
Bahha🐧 I see... however the code name as well as the function name seems to me enough explicit ;)
3rd Jun 2021, 9:48 AM
visph
visph - avatar
+ 1
lisa Yes, Some test cases examples array_diff([1,2], [1]), [2], "a was [1,2], b was [1], expected [2]") array_diff([1,2,2], [1]), [2,2], "a was [1,2,2], b was [1], expected [2,2]") array_diff([1,2,2], [2]), [1], "a was [1,2,2], b was [2], expected [1]") array_diff([1,2,2], []), [1,2,2], "a was [1,2,2], b was [], expected [1,2,2]") array_diff([], [1,2]), [], "a was [], b was [1,2], expected []") array_diff([1,2,3], [1, 2]), [3], "a was [1,2,3], b was [1, 2], expected [3]")
3rd Jun 2021, 8:54 AM
Lambda
Lambda - avatar
+ 1
Use a = a.pop(a[j])
3rd Jun 2021, 8:55 AM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 1
you are using range() when the length of the list changes with pop(), it will go out of index range.
3rd Jun 2021, 8:59 AM
Bahhaⵣ
Bahhaⵣ - avatar
+ 1
visph Thanks this one seems much efficient
3rd Jun 2021, 9:02 AM
Lambda
Lambda - avatar
+ 1
Bahha🐧 your 'set' solution would fail for: x = [1,1,2,2,3] z = [2] as result would be: [1,3] but expected is [1,1,3] ^^
3rd Jun 2021, 9:21 AM
visph
visph - avatar
+ 1
visph wow thank you so much guys, from the factor of O(n) your solution seems much faster and efficient.
3rd Jun 2021, 9:21 AM
Lambda
Lambda - avatar
+ 1
Scooby you doesn't need the try..except statement, as you remove value only 'while i in a'... def array_diff(a,b): for i in b: while i in a: a.remove(i) return a that's what I suggested in my first post, without removing the unuseful first tests ;P
3rd Jun 2021, 9:45 AM
visph
visph - avatar
+ 1
Scooby Here's a possible solution: def arr_dif(a, b): return [x for x in a if x not in b] # Hope this helps
4th Jun 2021, 4:48 PM
Calvin Thomas
Calvin Thomas - avatar
- 3
Hi
5th Jun 2021, 8:41 AM
Lion legend gamer