array1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
array2 = [1, 2, 2, 2, 5, 6, 6, 6, 9]
temp = set(array2)
array1.remove(temp)
Traceback (most recent call last):
File "Sudoku V2.py", line 6, in <module>
array1.remove(temp)
ValueError: list.remove(x): x not in list
Try this:
array1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
array2 = [1, 2, 2, 2, 5, 6, 6, 6, 9]
set(array1).difference(array2)
=> set([3, 4, 7, 8])
The above makes use of the difference() method of sets, which returns a new set with elements in the set that are not in the iterable(s) received as parameter. Notice that there's no need to convert array2 to a set for this to work.
Also be aware that by using sets, all duplicate elements will be removed and the original order of the iterables won't necessarily be preserved. If that's an issue, try this alternative solution:
[x for x in array1 if x not in array2]
=> [3, 4, 7, 8]
If you don't care about the order of elements in the result, or about duplicate elements, set.difference, as in Óscar López's answer, is exactly what you want.
If you do care about order and preserving duplicates, the simplest way is to just build a new list, filtering out the ones in array2:
set2 = set(array2)
array1 = [item for item in array1 if item not in set2]
If you need to destructively modify array1 in-place instead of building a new one, it's almost always simpler to just destructively replace the contents of array1 all at once:
array1[:] = [item for item in array1 if item not in set2]
If you really want to modify it element by element, the best way would be to iterate array1 backward, removing in-place:
set2 = set(array2)
for i, value in enumerate(array1)[::-1]:
if value in set2:
del array1[i]
Finally, you can do it the way flornquake suggested, but that will take quadratic time instead of linear, because it takes no advantage of the set; for each element in set2, it does a linear array1.remove.
Do you want array1 - array2?
set1 = set(array1)
set2 = set(array2)
set1.difference(set2) is what you want.
s -> sequence
s.remove(x) remove the first item from s where s[i] == x
remove raises ValueError when x is not found in s.
http://docs.python.org/3/library/stdtypes.html#index-23
None of the elements in array1 is a set. All the elements in array1 are integers.
The remove() method takes a single element of the list, so we have to call it once for each element:
for item in set(array2):
array1.remove(item)
Other methods such as Óscar's and abanert's are better though, they are more idiomatic and a lot more efficient.
This is three solution that can be help to your question:
array1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
array2 = [1, 2, 2, 2, 5, 6, 6, 6, 9]
array = [i for i in array1 if i not in array2]
print(array)
o = list(set(array1) - set(array2))
print(o)
from collections import OrderedDict
p = OrderedDict()
p['array'] = list(set(array1) - set(array2))
print(*p.values())
Related
in this code I'm trying to delete every repeated element in the list and just make all of the elements unique and not repeated, so when I run this code give me an error:
myList = [1, 2, 4, 4, 1, 4, 2, 6, 2, 9]
repeat = 0
for i in range(len(myList)-1):
for j in range(len(myList)-1):
if myList[i]== myList[j]:
repeat+=1
if repeat>1:
del myList[j]
print("The list with unique elements only:")
print(myList)
the error which apppears is :
Traceback (most recent call last):
File "main.py", line 8, in <module>
if myList[i]== myList[j]:
IndexError: list index out of range
why is that happens and how can I solve it?
It is a really bad idea to modify an array while looping on it as you have no control on the way things are handled.
May I suggest these two solutions to your problem.
The first one is using set.
myList = [1, 2, 4, 4, 1, 4, 2, 6, 2, 9]
myList = list(set(myList))
print("The list with unique elements only:")
print(myList)
The other solution is using an other array
myList = [1, 2, 4, 4, 1, 4, 2, 6, 2, 9]
uniques = []
for number in myList:
if number not in uniques:
uniques.append(number)
print("The list with unique elements only:")
print(uniques)
You can convert list to set, it will automatically delete all of repeated elements
a = [1, 2, 4, 4, 1, 4, 2, 6, 2, 9]
unique_list = list(set(a))
print(a)
Note: We again convert set to list
What is heppening here is that you are deleting some elements in your list, making it shorter.
Since your for loops are running for the lenght of your original list, you will eventuall try to access an index that no longer exists. This will cause you to get "list index out of range"
To see this for your self, you can add a print statement, like so:
myList = [1, 2, 4, 4, 1, 4, 2, 6, 2, 9]
repeat = 0
for i in range(len(myList)-1):
for j in range(len(myList)-1):
print(i,j,len(myList))
if myList[i]== myList[j]:
repeat+=1
if repeat>1:
del myList[j]
Set data type in Python is used to remove duplicity. Whenever any iterator needs to be viewed with only the unique values in it, it can be converted into a set and that will remove all the duplicate values. For example:
lis=[2,2,3,4]
l=set(lis)
print(l)
Output:
{2, 3, 4}
It can be converted back into the list:
lis=[2,2,3,4]
l=set(lis)
print(l)
l=list(l)
print(l)
Output:
{2, 3, 4}
[2, 3, 4]
Similarly:
myList = [1, 2, 4, 4, 1, 4, 2, 6, 2, 9]
s=set(myList)
l=list(s)
print(l)
Output:
[1, 2, 4, 6, 9]
Frozen sets can also be used for this purpose. Although; elements of the frozen set remain the same after creation i.e, they can't be modified unlike the elements of the set which are mutable(can be modified).
Hope this was helpful!
I know that you are not supposed to remove an element of a list while iterating over it but I have to.
I'm trying to iterate over a list of lists and if I find a value in a list of my lists i need to remove it.
This is what I've tried so far.
dict[["A1","A2"],
["B1","B2"],
["C1","C2"]]
for i in range(len(dict)):
if dict[i][0]=="A1":
dict.pop(i)
But it's giving me an error of out of range.
How can I do it with list comprehensions or any other approach?
Do you mean this?
old = [["A1","A2"], ["B1","B2"], ["C1","C2"]]
new = [x for x in old if x[0] != "A1"]
You can't. You will get an exception. Create a new list as a copy.
>>> disallowed = [1, 2, 3]
>>> my_list = [ [1, 2, 3, 4, 5, 6, 7], [3, 3, 4, 5, 8, 8, 2] ]
>>> filtered_list = [[y for y in x if y not in disallowed] for x in my_list]
>>> print filtered_list
[[4, 5, 6, 7], [4, 5, 8, 8]]
You can actually delete from a list while you iterate over it, provided you to it backwards (so deletion only affects higher indices which you have already seen during this iteration):
data = [["A1","A2"],
["B1","B2"],
["C1","C2"]]
for i, pair in reversed(data):
if pair[0] == 'A1':
del data[i]
Have a list arr = [1,3,4,5,2,3,4,2,5,7,3,8,1,9,6,2,1,2,1,3,4,3,4,6,9]
want to remove the duplicate values so that the original list should contains single instances of all elements. Do not want to create a extra list and append the elements from list. Also do not want to use inbuilt "set".
Tried to do that with some code as below:
l = len(arr)
for x in range(l):
for y in range(x+1,l):
if arr[x] == arr[y]:
del arr[y]
Tried the above code and its throwing error
"IndexError: list index out of range"
What I understand is whiling deleting the value the size of the list is changing for which its throwing the error. So I made the below changes. But still its failing with same error:
l = len(arr)
for x in range(l):
for y in range(x+1,l):
if arr[x] == arr[y]:
t = y
del arr[y]
y = t - 1
Can some one help me out on this?
Thanks in Advance.
You are trying to make the code more efficient by caching the length of the list in the local variable l. However, that is not helpful because the list is being trimmed inside the loop, and you are not keeping the cached length variable in sync.
for index in range(len(arr)-1,0,-1):
if arr[index] in arr[:index]:
del arr[index]
By going backwards through the array and looking for earlier occurrences of each element, you can avoid having to worry about the length of the list changing all the time.
This method also preserves the order in which elements occur in the original array. Note the instruction is to only remove duplicates (a.k.a. subsequent occurrences).
For example the list [9,3,4,3,5] should reduce to [9,3,4, 5] as the second occurrence of 3 is considered a duplicate and should be removed.
How about this approach:
>>> set(arr)
set([1, 2, 3, 4, 5, 6, 7, 8, 9]) #Just to compare it with the results below.
>>> arr = [1,3,4,5,2,3,4,2,5,7,3,8,1,9,6,2,1,2,1,3,4,3,4,6,9]
>>> arr.sort()
>>> arr
[1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 6, 6, 7, 8, 9, 9]
>>> for i in arr:
while arr.count(i) > 1:
del arr[i]
>>> arr
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Another approach is to find, after sorting your list, the length of the sublist to delete for each number:
>>> arr = [1,3,4,5,2,3,4,2,5,7,3,8,1,9,6,2,1,2,1,3,4,3,4,6,9]
>>> arr.sort()
>>> arr
[1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 6, 6, 7, 8, 9, 9]
>>> for i,j in enumerate(arr):
del arr[i+1:i+arr.count(j)]
>>> arr
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Why use extend when you can just use the += operator? Which method is best?
Also what's the best way of joining multiple lists into one list
#my prefered way
_list=[1,2,3]
_list+=[4,5,6]
print _list
#[1, 2, 3, 4, 5, 6]
#why use extend:
_list=[1,2,3]
_list.extend([4,5,6])
print _list
#[1, 2, 3, 4, 5, 6]
_lists=[range(3*i,3*i+3) for i in range(3)]
#[[0, 1, 2], [3, 4, 5], [6, 7, 8]]
#my prefered way of merging lists
print sum(_lists,[])
#[0, 1, 2, 3, 4, 5, 6, 7, 8]
#is there a better way?
from itertools import chain
print list(chain(*_lists))
#[0, 1, 2, 3, 4, 5, 6, 7, 8]
+= can only be used to extend one list by another list, while extend can be used to extend one list by an iterable object
e.g.
you can do
a = [1,2,3]
a.extend(set([4,5,6]))
but you can't do
a = [1,2,3]
a += set([4,5,6])
For the second question
[item for sublist in l for item in sublist] is faster.
see Making a flat list out of list of lists in Python
You may extend() a python list with a non-list object as an iterator. An iterator is not storing any value, but an object to iterate once over some values. More on iterators here.
In this thread, there are examples where an iterator is used as an argument of extend() method: append vs. extend
I have 2 arrays:
arr1 = [a,b,c,d,e]
arr2 = [c,d,e]
I want to give array arr1 except arr2.
Mathematically, you're looking for a difference between two sets represented in lists. So how about using the Python set, which has a builtin difference operation (overloaded on the - operator)?
>>>
>>> arr = [1, 2, 3, 4, 5]
>>> arr2 = [3, 4, 9]
>>> set(arr) - set(arr2)
>>> sdiff = set(arr) - set(arr2)
>>> sdiff
set([1, 2, 5])
>>> list(sdiff)
[1, 2, 5]
>>>
It would be more convenient to have your information in a set in the first place, though. This operation suggests that a set better fits your application semantics than a list. On the other hand, if you may have duplicates in the lists, then set is not a good solution.
So you want the difference of two lists:
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list2 = [1, 2, 3, 4, 4, 6, 7, 8, 11, 77]
def list_difference(list1, list2):
"""uses list1 as the reference, returns list of items not in list2"""
diff_list = []
for item in list1:
if not item in list2:
diff_list.append(item)
return diff_list
print list_difference(list1, list2) # [5, 9, 10]
Or using list comprehension:
# simpler using list comprehension
diff_list = [item for item in list1 if item not in list2]
print diff_list # [5, 9, 10]
If you care about (1) preserving the order in which the items appear and (2) efficiency in the case where your lists are large, you probably want a hybrid of the two solutions already proposed.
list2_items = set(list2)
[x for x in list1 if x not in list2_items]
(Converting both to sets will lose the ordering. Using if x not in list2 in your list comprehension will give you in effect an iteration over both lists, which will be inefficient if list2 is large.)
If you know that list2 is not very long and don't need to save every possible microsecond, you should probably go with the simple list comprehension proposed by Flavius: it's short, simple and says exactly what you mean.