This question already has answers here:
How to remove multiple indexes from a list at the same time? [duplicate]
(8 answers)
Deleting multiple elements from a list
(32 answers)
Closed 4 years ago.
I have a list of values for example:
[1,2,3,4,5,6,7,8,9,10]
And specific indexes like:
[0,3,5]
I want something that returns deleting the values which the index belongs to the [0,3,5] array:
[2,3,5,7,8,9,10]
Any ideas for Python 3?
Thanks
Using enumerate and list comprehension.
Ex:
l = [1,2,3,4,5,6,7,8,9,10]
toDelete = [0,3,5]
print([v for i,v in enumerate(l) if i not in toDelete])
Output:
[2, 3, 5, 7, 8, 9, 10]
Related
This question already has answers here:
python: getting all pairs of values from list
(2 answers)
Closed 1 year ago.
I have a list [a,b,c,d] and I want to produce a list that gives me the absolute value of the substractions of every two elements.
[|a-b|, |a-c|, |a-d|, |b-c|, |b-d|, |c-d|]
Is there a function or a nice pythonic 1-liner to do this?
Have you tried something like this? This is a quick way to get all combinations in a list:
>>> x = [1, 4, 9, 16]
>>> [abs(x[i]-x[j]) for i in range(len(x)) for j in range(i+1, len(x))]
[3, 8, 15, 5, 12, 7]
This question already has answers here:
How to apply a function to each sublist of a list in python?
(5 answers)
Closed 3 years ago.
So I know how to get the sum of a single list so say,
a=[1,2,3,4]
sum(a)
10
How would I go about trying to sum lists in a list of lists? So, from:
[[1,2],[2,3],[3,42]] to
[3,5,45]?
Code:
l = [[1,2],[2,3],[3,42]]
print([sum(i) for i in l])
Output:
[3, 5, 45]
You should use sum() in list comprehension for other lists
In [12]: a = [[1,2],[2,3],[3,42]]
In [13]: [sum(i) for i in a]
Out[13]: [3, 5, 45]
intial_list = [[1,2],[2,3],[3,42]]
res = list(map(sum, intial_list))
print(res)
output
[3,5,45]
This question already has answers here:
Strange result when removing item from a list while iterating over it
(8 answers)
Closed 3 years ago.
I am trying to remove duplicate elements from a list. The code is removing the wrong elements.
def simplify(A):
for i in A:
for j in A:
if i == j:
A.remove(j)
return A
Input A:
A = [1, 2, 5, 8, 12, 5]
Output:
A = [2, 8]
Try this.
def RemoveDuplicates(list_):
return list(set(list_))
You can call this function by passing your list and you get back listwithout duplicates.
This question already has answers here:
How to remove items from a list while iterating?
(25 answers)
Removing from a list while iterating over it [duplicate]
(5 answers)
Closed 5 years ago.
I have the next piece of code which counts unique elements in list and remove those elements from that list:
l = [1, 2, 3, 4, 5]
for i in l:
if l.count(i) == 1:
l.remove(i)
print l
However, after list modification by l.remove(i) I still have elements in the list: [2, 4]
My question is why list.remove() operation doesn't delete all elements if all of them are unique and amount per each one will be 1 during each iteration?
This question already has answers here:
Remove all the elements that occur in one list from another
(13 answers)
Closed 6 years ago.
I'm stuck with a part in one of my codes where I have to delete all the occurances present in listA that are identical in listB.
Example:
A=[1,4,4,4,3,3,2,1,5,5]
B=[4,3]
Result should be A=[1,2,1,5,5]. Ideally I would want to do it in linear time.
using Set Operations:
list(set(A) - set(B))
Using List Comprehension
list(set([i for i in A if i not in B]))
Try with list comprehension,
In [11]: [i for i in A if i not in B]
Out[11]: [1, 2, 1, 5, 5]