Delete a set of positions from a Python list [duplicate] - python

This question already has answers here:
How to remove multiple indexes from a list at the same time? [duplicate]
(8 answers)
Closed 5 years ago.
Although this should be rather easily done with a for loop, I'm wondering wether there is a concise way to delete a set of positions from a Python list. E.g.:
l = ["A","B","C","D","E","F","G"]
pos = [4,6]
# Is there something close to
l.remove(pos)
Best

del is what you are looking for if you have consecutive indices:
From the documentation:
There is a way to remove an item from a list given its index instead of its value: the del statement
Otherwise, you could generate a new list, by filtering out indices, which you aren't interested in
result = [x for i, x in enumerate(l) if i not in pos]

Related

Find the biggest value in a given list [duplicate]

This question already has answers here:
How to find the maximum number in a list using a loop?
(11 answers)
Closed 2 years ago.
by using only a temporary variable, a for loop, and an if to compare the values-hackinscience.org
Find the biggest value in a given list.
the_list = [
143266561,
1738152473,
312377936,
1027708881,
1495785517,
1858250798,
1693786723,
1871655963,
374455497,
430158267,
]
max_in = 0
for val in the_list:
if val > max_in:
max_in = val
There is the for, there is the if, max_in is somehow a temp var cause it changes over the loop. You get it.
No need for either of that. Use max(the_list).

How to remove element from a list using other list? [duplicate]

This question already has answers here:
Deleting multiple elements from a list
(32 answers)
Closed 3 years ago.
I have two list
a =[72,82,955,55,.....]
and
b=[5,7,8...]
I want to remove the element from a. list b specifies the indexes from where elements to be removed.
You could use a list comprehension in combination with enumerate method in order to apply condition based on the index.
result = [item for index, item in enumerate(a) if index not in b]

Perform an operation to each item of a list [duplicate]

This question already has answers here:
Divide elements of a list by integer with list comprehension: index out of range
(5 answers)
Closed 3 years ago.
I have a list of numbers and i need to divide them all by a specific number.
Lets say i want to divide all of the items by 2
list = [1,2,3,4,5,6,7]
wanted_list = [1/2,2/2,3/2,4/2,5/2,6/2,7/2]
I attempted a for loop that changes each but it didnt work for some reason like it didnt do the operation.
list = [1,2,3,4,5,6,7]
wanted_list = [i/2 for i in list]
print(wanted_list)
I assume san's answer is the best approach, as it utilizes lists comprehension and in that case a new list is created automatically, however considering what you wrote, you can as well use a for loop, only you need to store the result somewhere, ex:
data = [2, 4, 6]
wanted_data = []
for d in data:
wanted_data.append(int(d/2))
print(wanted_data)

Change list to set without changing order of elements [duplicate]

This question already has answers here:
How do I remove duplicates from a list, while preserving order?
(30 answers)
Closed 3 years ago.
I want to remove those element from list which has repetition more than one it's like set but order should not be change
[4,6,2,6,1,2] should become [4,6,2,1]
I'am looking for any inbuilt method or list comprehension
This should do it: use lambda function and remove duplicates using set. Tested on Python 2.7
mylist = [4,6,2,6,1,2]
reduce(lambda r, v: v in r[1] and r or (r[0].append(v) or r[1].add(v)) or r, mylist, ([], set()))[0]

Changing list while iterating [duplicate]

This question already has answers here:
Modifying a list while iterating over it - why not? [duplicate]
(4 answers)
Closed 6 years ago.
I've found a python puzzle and can't find out why it works.
x = ['a','b','c']
for m in x:
x.remove(m)
and after this loop x = ['b'].
But why?
As far as I understand for keyword implicitly creates iterator for this list. Does .remove() calls __next__() method so b is skipped? I can't find any mentions of it but this is my best guess.
Here you are iterating over the original list. On the first iteration, you removed the 0th index element i.e. a. Now, your list is as: ['b','c']. On the second iteration your for loop will access the value at index 1 but your index 1 has value c. So the c is removed. Hence resultant list will be ['b'].
In order to make it behave expectedly, iterate over the copy of the list, and remove the item from original list. For example:
x = ['a','b','c']
for m in list(x): # <-- Here 'list(x)' will create the copy of list 'x'
# for will iterate over the copy
x.remove(m)
# updated value of 'x' will be: []
Note: If it is not for demo purpose and you are using this code for emptying the list, efficient way of emptying the list will be:
del x[:]

Categories