How can I find the index of the duplicate element in the array?
mylist = [1,2,3,4,5,6,7,8,9,10,1,2]
How can I find the index number of 1 and 2 repeated array elements here?
So, not the first duplicate element, I want to find and delete the trailing duplicate element from the array. 1 2 values at the end.
Thanks for your responses.
Single pass remove duplicates:
mylist = [1,2,3,4,5,6,7,8,9,10,1,2]
def remove_duplicates(l):
seen = {}
res = []
for item in l:
if item not in seen:
seen[item] = 1
res.append(item)
return res
print(remove_duplicates(mylist))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Which also preserves order:
mylist = [1,10,3,4,5,6,7,8,9,2,1,2]
print(remove_duplicates(mylist))
[1, 10, 3, 4, 5, 6, 7, 8, 9, 2]
The simplest way to do this (unless you want to log where the duplicates are) is to convert the list to a set.
A set can contain only one instance of each value.
You can do this very easily as shown below.
Note: A set is respresented with curly braces { like a dictionary, but does not have key,value pairs.
mylist = [1,2,3,4,5,6,7,8,9,10,1,2]
myset = set(mylist)
// myset = {1,2,3,4,5,6,7,8,9,10}
EDIT: If the order is important, this method will not work as a set does not store the order.
You can find duplicates in a cycle, memorizing previously seen values in a set.
mylist = [1,2,3,4,5,6,7,8,9,10,1,2]
myset = set()
indices_of_duplicates = []
for ind, val in enumerate(mylist):
if val in myset:
indices_of_duplicates.append(ind)
else:
myset.add(val)
Then you can delete duplicate elements with
for ind in reversed(indices_of_duplicates):
del mylist[ind]
Note that we are deleting elements starting from the end of the list because otherwise we would shift elements and we would have to update the indices we have found.
eh, I did this:
a=[1,2,3,4,5,5,6,7,7,8]
b=list(set(a))
tr=0
al=len(a)
bl=len(b)
iz=[]
if al != bl:
for ai in range (0,al):
ai0=a[ai]
bi0=b[ai]
if ai0 != bi0:
iz.append(ai)
b.insert(ai,ai0)
then the list iz is all the i values where they don't match, so you just plug those back in.
just wanted to share!
If you have to remove duplicates you can use Set()
mylist = [x for x in set([1,2,3,4,5,6,7,8,9,10,1,2])]
Related
I have a list of values and I want to move certain (or all) values to another list if they exist in a reference list.
x = [2,3,4,5,6,7,8] # list of values
ref = [2,3,4,5,6,7,8] # reference list
result = [x.pop(i) for i, v in enumerate(x) if v in ref]
But because of popping the current index, it ends up giving every other value instead. Is there a nice straightforward way to do this?
What I want at the end of this example is x=[] and result=[2,3,4,5,6,7,8], but ref doesn't need to contain all elements of x, this was just for an example. In another case it might be:
x = [2,3,4,5,6,7,8] # list of values
ref = [2,6,7] # reference list
So then I want x = [3,4,5,8] and result = [2,6,7]
In general, we should avoid modifying objects while iterating over them.
For this problem, we could generate result and filter x in two steps using comprehensions (avoiding appending to lists) as in the following example.
result, x = [v for v in x if v in ref], [v for v in x if v not in ref]
You could do it the old-fashioned way, with a while loop and a pointer into x:
x = [2, 3, 4, 5, 6, 7, 8]
ref = [2, 6, 7]
result = []
i = 0
while i < len(x):
if x[i] in ref:
result.append(x.pop(i))
else:
i += 1
print(x)
print(result)
Output:
[]
[2, 3, 4, 5, 6, 7, 8]
You can simply iterate from the end to the start to avoid pop() changing the list size while iterating. Just call reverse() on your new list after running your loop if the order of the list matters.
Let's say i have list output of 500 items. What i want is getting the previous value which is not the same of the last item.
For example:
List = [1,7,9,8,5,5,5,5,5,5,5,5,5]
desired output : 8
I need my code the calculate previous different item. In my list, the last item is 5 and according to previous items, i need the first different number than the last one.
I searched google, but all i could find is the previous item compared to last or previous low/high values in the list which are not helping me at the moment.
Thanks a lot.
One approach using extended iterable unpacking and next:
lst = [1, 7, 9, 8, 5, 5, 5, 5, 5, 5, 5, 5, 5]
last, *front = reversed(lst)
res = next((v for v in front if last != v), None)
print(res)
Output
8
The above approach is equivalent to the following for-loop:
last, *front = reversed(lst)
res = None
for v in front:
if last != v:
res = v
break
print(res)
Another solution would be to transfer the list into a dict (dicts are guaranteed to be ordered since Python 3.7), reconvert the resulting keys into a list and take the second last item:
>>> l = [1,7,9,8,5,5,5,5,5,5,5,5,5]
>>> list(dict.fromkeys(l))[-2]
8
Note: this is probably quite slow for larger lists and not that readable, but I thought it would be fun
I'm new to Python so I don't know if this is possible, but my guess is yes. I want to iterate over a list and put items into new lists according to their value. For instance, if item_x == 4, I would want to put it in a list called list_for_4. The same is true for all other items in my list and numbers 0 to 10. So is it possible to generalize a statement in such a way that if item_x == *a certain value*, it will be appended to list_for_*a certain value*?
Thanks!
Maybe use list comprehension with if statement inside?
Like:
list_for_4 = [x for x in my_list if x==4]
And combine it with a dict.
With a simple iteration through the list:
lst_for_1 = []
lst_for_2 = []
lst_for_3 = []
d = {1: lst_for_1, 2: lst_for_2, 3: lst_for_3}
for x in lst:
d[x].append(x)
Or, if you want a condition that is more complicated than just the value of x, define a function:
def f(x):
if some condition...:
return lst_for_1
elif some other condition:
return lst_for_2
else:
return lst_for_3
Then replace d[x].append(x) by f(x).append(x).
If you don't want to do the iteration yourself, you could also use map:
list(map(lambda x: d[x].append(x),lst))
or
list(map(lambda x: f(x).append(x),lst))
The version with map will return an list of Nones that you don't care about. map(...) returns an iterator, and as long as you do not iterate through it (for example, to turn its result into a list), it will not perform the mapping. That's why you need the list(map(...)), it will create a dummy list but append the items of lst to the right lists on the way, which is what you want.
Don't know why you want to do it. But you can do it.
data = [1, 2, 3, 4, 1, 2, 3, 5]
for item in data:
name = f'list_for_{item}'
if name in globals():
globals()[name].append(item)
else:
globals()[name] = [item]
Instead of trying to generate a dynamic variables. A map using a dictionary structure might help you.
For example:
from collections import defaultdict
item_list = [1, 2, 3, 9, 2, 2, 3, 4, 4]
# Use a dictionary which elements are a list by default:
items_map = defaultdict(list)
for i in item_list:
items_map['list_for_{}'.format(i)].append(i)
print(items_map)
# Test the map for elements in the list:
if 'list_for_4' in items_map:
print(items_map['list_for_4'])
else:
print('`list_for_4` not found.')
Alternatively if you only require the number of times an item occurs in the list you could aggregate it using Counter:
from collections import Counter
item_list = [1, 2, 3, 9, 2, 2, 3, 4, 4]
result = Counter(item_list)
print(result)
This question already has answers here:
Removing elements that have consecutive duplicates
(9 answers)
Closed 3 years ago.
I have a list like this,
l=[1,1,1,1,2,2,2,3,3,3,3,3,3,4,4,5,6,6,6,5,5,5,7,7,8,8,8,8,8,9,9,9,10,10]
Now I want to select only one values of repeating value continuously, so the output should look like,
l=[1,2,3,4,5,6,5,7,8,9,10]
I could do this using a for loop checking the next values with the previous and append to a list, But the execution time is huge in that case, Looking for shortcuts to do it most efficiently.
You can use itertools.groupby to group consecutive values and keep only the grouping key:
from itertools import groupby
[k for k,_ in groupby(l)]
# [1, 2, 3, 4, 5, 6, 5, 7, 8, 9, 10]
Use the set data structure to remove duplicates and convert it back to a list:
l = [1,1,1,1,2,2,2,3,3,3,3,3,3,4,4,5,6,6,6,5,5,5,7,7,8,8,8,8,8,9,9,9,10,10]
# remove duplicates
num = set(l)
# convert the set of unique values back to a list
num = list(num)
print(num)
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
You can use itertools.groupby or something like this (I haven't used zip or slices on purpose and tried to keep it as simple as possible)
def group_by(array):
if not array:
return array
prev = array[0]
result = [prev]
for el in array:
if el != prev:
result.append(el)
prev = el
return result
P.S. Yes, with zip or enumerate or islice it can be more elegant, I know :-)
I am curious. How can I correctly iterate through a list, compare two values and delete the duplicate if it exists.
Here I created a nested for loop:
my_list = [ 1, 2, 3, 4, 5 ]
temp = [1, 5, 6]
def remove_items_from_list(ordered_list, temp):
# Removes all values, found in items_to_remove list, from my_list
for j in range(0, len(temp)):
for i in range(0, len(ordered_list)):
if ordered_list[i] == temp[j]:
ordered_list.remove(ordered_list[i])
But when I execute my my code I get an error:
File "./lab3f.py", line 15, in remove_items_from_list
if ordered_list[i] == items_to_remove[j]:
can anyone explain why?
This question, wanted to me compare two lists with one another, and these lists have two different lengths. If an item in list a matched a value in list b, we wanted then to delete it from list a.
You actually can remove items from a list while iterating over it but do read links by #ReblochonMasque.
Here is one way of removing duplicates:
def remove_items_from_list(ordered_list, temp):
n = len(ordered_list)
for i in range(n - 1, -1, -1):
if ordered_list[i] in temp:
del ordered_list[i]
Then
>>> remove_items_from_list(my_list, temp)
>>> print(my_list)
[2, 3, 4]
However, one of the easiest ways of solving your problem is to use sets:
list(set(my_list) - set(temp))
When using this approach, order of items in the resulting list may be arbitrary. Also, this will create a new list instead of modifying an existing list object. If order is important - use list comprehension:
[v for v in my_list if v not in temp]
While you iterating your loop, you remove item from orderer_list which cause index error
Try this:
def remove_items_from_list(ordered_list, temp):
list_ = [x for x in orderer_list if x not in temp]
return list_
first find the duplicated elements and then remove them from the original list.
dup_list = [item for item in temp if item in my_list]
for ele in dup_list:
my_list.remove(ele)
remove() source
You can't remove an items from the list you are iterating over. You can create a copy of the array and remove items from it.