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]
Related
This question already has answers here:
Find indexes of common items in two python lists
(3 answers)
Closed 1 year ago.
Assume we have two lists, A and B. Is there a way to get the indices of elements in the list B, which are in the list A?
For example:
A = [1,2,3,4]
B = [3,4,1,2,5]
The result should be:
[2,3,0,1]
Could it be implemented without for-loop (or fast)?
This should work for your use case:
result = [A.index(x) for x in B if x in A]
Use index function
A = [1,2,3,4]
B = [3,4,1,2,5]
lst=[]
for i in A:
res=B.index(i)
lst.append(res)
print(lst)
# [2, 3, 0, 1]
This question already has answers here:
How to remove items from a list while iterating?
(25 answers)
Closed 4 years ago.
I used a for loop to remove the duplicates in a list from the left side. For example, [3,4,4,3,6,3] should be [4,6,3]. And also the sequence cannot be changed, that's why I cannot use a set here. If I use set() here, the result would be [3,4,6].
def solve(arr):
for i in arr:
if i in arr[arr.index(i)+1:]:
arr.remove(i)
return arr
solve([1,2,1,2,1,2,3])
It should return [1,2,3]. Instead, I got [2,1,2,3]. I viewed the visualized execution of the code, and it seems when python got to the item '2' the second time, meaning when the arr is mutated to be [2,1,2,3], it just suddenly stopped iteration from the for loop and returned the list as it is. When it should detect there is still another '2' in the list. Please help
You can use set for this:
a = [1,2,1,2,1,2,3]
b = set(a)
Then b will be:
{1,2,3}
You can try this way:
def solve(arr):
another_arr = []
for i in arr:
if i in another_arr:
another_arr.remove(i)
another_arr.append(i)
else:
another_arr.append(i)
return another_arr
print(solve([1, 2, 1, 2, 1, 2, 3]))
This question already has answers here:
Access multiple elements of list knowing their index [duplicate]
(11 answers)
Closed 5 years ago.
Say I have a list:
[0,1,2,3,4,5,6,7,8,9]
From this list I want to retrieve the centre elements at positions 4, 5 and 6. So: [4,5,6] is the output I want from this list.
I know how to remove elements, that's easy using this method:
x = [position1:] + [:position2]
But I don't know how to find those items.
Use a slice:
l = [0,1,2,3,4,5,6,7,8,9]
print(l[4:7]) # [4, 5, 6]
This question already has answers here:
Create list of single item repeated N times
(9 answers)
Closed 5 years ago.
I need to get same raw multiple times like bellow.
A=[2,3,4,5]
B=[[2,3,4,5],[2,3,4,5],[2,3,4,5],[2,3,4,5],...,[2,3,4,5]]
Number of rows can be change.
How can I do this problem?
Try this:
B = [[i for i in A] for n in range(5)]
Change the value in the range () call to change the number of copies. This doesn't suffer from reference sharing like the other answers.
online example
Or a shorter equivalent:
B = [A[:] for n in range(5)]
This also doesn't suffer from reference sharing because the slice operator creates a deep copy* as well.
online example
* at least in terms of this input
As has been pointed out, just doing list comprehension in the simplest way will only make multiple references to the same old list. To instead ensure we get a new copy of the list, we can do:
A = [2, 3, 4, 5]
B = [list(A) for i in range(10)]
where list(A) ensures that we get a copy of A instead of a reference to the same list.
You can also do:
B=([A.copy()]*10)
However, it can produce references between the lists in B if their elements are complex object. But if you're using it for ints, strings and others basic stuffs it's going to work well.
A = [1,2,3]
B= []
n= 5
for i in range(5):
B.append(A)
where A is list, B is resultant list and n is number of rows
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.