This question already has answers here:
Removing duplicates in lists
(56 answers)
Closed 8 years ago.
Given a list of n numbers how can i print each element except the duplicates?
d = [1,2,3,4,1,2,5,6,7,4]
For example from this list i want to print : 1,2,3,4,5,6,7
Since order doesn't matter, you can simply do:
>>> print list(set(d))
[1, 2, 3, 4, 5, 6, 7]
It would be helpful to read about sets
If the order does not matter:
print set(d)
If the type matters (want a list?)
print list(set(d))
If the order matters:
def unique(d):
d0 = set()
for i in d:
if not i in d0:
yield i
d0.add(i)
print unique(d)
All you have to do is
create an array.
get list's element.
if element exists in array, leave it.
and if it does not exists, print it.
Related
This question already has answers here:
How to remove items from a list while iterating?
(25 answers)
Closed 7 months ago.
I have written a script
def remove_every_other(my_list):
# Your code here!
rem = False
for i in my_list:
if rem:
my_list.remove(i)
rem = not rem
return my_list
It removes every other element from the list.
I input [1,2,3,4,5,6,7,8,9,10] it returns [1,3,4,6,7,9,10]
Which to me is very strange also if I input [Yes,No,Yes,No,Yes]
it outputs [Yes,No,Yes,No]
I have tried to figure it out for hours but couldn't get it to work, I am a beginner.
You could just use slicing for that. Or do you want to do it explicitly in a loop? For an explanation of the syntax, you can follow the link. Basically you take the full list (no start or end defined) with a step-value of 2 (every other element). As others have pointed out, you run into problems if you're modifying a list that you're iterating over, thus the unexpected behavior.
input_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
output_list = input_list[::2]
print(output_list)
This returns a copy of the list with every other element removed:
[1, 3, 5, 7, 9]
Since .remove() is being called upon the same list that you're iterating over, the order is getting disarranged.
We could append the items that you want at the end in another list to maintain the ordering in the original list.
def remove_every_other(my_list):
morphed_list = []
rem = True
for i in my_list:
if rem:
morphed_list.append(i)
rem = not rem
return morphed_list
In general, it is not a good idea to modify the list you're iterating over. You could read more about it over here: https://stackoverflow.com/questions/10812272/modifying-a-list-while-iterating-over-it-why-not#:~:text=The%20reason%20to%20why%20you,list%20of%20just%20odd%20numbers.
This question already has answers here:
Removing duplicates in lists
(56 answers)
'order' of unordered Python sets
(5 answers)
Closed 3 years ago.
I tried to remove duplicates from a list in Python 3 by converting it into a set by using set(). However I tried to achieve a certain order at the end of the process. After converting the list, I noticed, that the resulting set was not in the order, I would have expected.
data = [3, 6, 3, 4, 4, 3]
my_set = set(data)
print(my_set)
The resulting set is: (3,4,6)
I expected set() to kind of iterate over the given list from 0 to n, keeping the first instance of every integer it encounters. However the resulting set seems to be ordered in a different way.
I was unable to find anything about this in the python documentation, or here on stack overflow. Is it known how the set() method orders the elements in the given datastructure when converting it to a set?
The concept of order simply does not exist for sets in Python, which is why you can not expect the elements to be shown in any particular order. Here is an example of creating a list without duplicates, that has the same order as the original list.
data = [3, 6, 3, 4, 4, 3]
without_duplicates = list(dict.fromkeys(data))
>>> without_duplicates
[3, 6, 4]
set objects are not ordered by key or by insertion order in Python... you can however get what you want by building the result you are looking for explicitly:
res = []
seen = set()
for x in data:
if x not in seen:
seen.add(x)
res.append(x)
print(res)
This question already has answers here:
Strange result when removing item from a list while iterating over it
(8 answers)
Closed 5 years ago.
A = [2,4,6,8,10,12]
for a in A:
if a%2 == 0: # If 2 divides a, remove a from A
A.remove(a)
print(A)
When I execute this block of code, the console output is [4,8,12].
My understanding of this code is that if any of the elements in [A] are divisible by 2, then we remove them from the list. In the list above, all elements are in fact divisible by 2, but only 2, 6, and 10 were removed. Anyone care to explain why 4, 8, and 12 were not removed?
Removing elements from a list while you're iterating through it messes up the iteration. You should use the filter function or a list comprehension instead.
This question already has answers here:
Deleting multiple elements from a list
(32 answers)
Closed 7 years ago.
I have an index of list elements that I want to delete. How can I do that ?
For example, if my original list is a=[1,2,3,4,5,6] and my index list is [2,3]. I want the elements 3,4 to be removed
Since you want to delete the indices, you can do the following two methods:
In place:
for index in sorted(indices, reversed=True):
del a[index]
out of place:
new_a = [el for index, el in enumerate(a) if index not in indices]
The reason why we sort for the in-place version is because deleting from the back doesn't modify the referenced elements in the front (note that this breaks with negative indexing).
a=[1,2,3,4,5,6]
a = a[:2] + a[4:]
print(a)
[1, 2, 5, 6]
This question already has answers here:
Does converting a list to a set change the order of elements?
(2 answers)
Closed 8 years ago.
In python if I have a list with numbers, that may have repeats, if I do the following am I guaranteed to get the same result?
l = #some list...
new_list = list(set(l))
I have tried it several times and it has given me the same result every time but I'm wondering if that is guaranteed. By same list I mean the same elements in the same order
Well, if you have duplicates, then no:
In [1]: l = [1,1,1]
In [2]: print list(set(l))
[1]
And the order can change as well:
In [5]: l = [3,2,1]
In [6]: print list(set(l))
[1, 2, 3]