Delete multiple values in a list of python [duplicate] - python

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]

Related

Is there a brief way to Get the indices of all elements from list A in list B [duplicate]

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]

Why doesn`t list[:][0] get me the first row of the list? [duplicate]

This question already has answers here:
Understanding slicing
(38 answers)
Closed 6 years ago.
For the following:
list=[[2, 3, 5], [7, 8, 9]]
Why does [list[0][0], list[1][0]] represent the first row ([2, 7]), but the command list[:][0] or list[0:2][0] returns the first column ([2, 3, 5])?
The way I see it list[:][0] should get all the possible values for the first parameter (that is 0 and 1) and 0 for the second, meaning it would return the first row. Instead what it does is return the first column and I can't understand why.
In python, the [a:b:c] syntax creates a new list. That is,
list = [1,2,3]
print(list[:])
is going to print a list, not a value.
Therefore, when you say list[:][0] you are making a copy of the original list (list[:]) and then accessing item 0 within it.
Of course you know, item 0 of the original list (list[0]) is another list.
I think you want:
[sl[0] for sl in list]
Elaboration:
This is called a "comprehension." It is a compact special syntax for generating lists, dicts, and tuples by processing or filtering other iterables. Basically {..}, [..], and (..) with an expression inside involving a for and optionally an if. Naturally, you can have multiples (like [x for x in y for y in z]) of the for, and multiples of the if.
In your case, it's pretty obvious you want a list. So []. You want to make the list by taking the first item from each sublist. So [sl[0] for sl in list].
Here's a more-detailed article: http://carlgroner.me/Python/2011/11/09/An-Introduction-to-List-Comprehensions-in-Python.html

Lists : printing number only if its not a duplicate [duplicate]

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.

Access two consecutive elements of a list in Python [duplicate]

This question already has answers here:
How do I split a list into equally-sized chunks?
(66 answers)
Closed 9 years ago.
I need to access the n and n+1 elements of a list. For example, if my list was [1,2,3,4,5] and my nth element was 2, I'd need the next element in the list, 3.
Specifically, I need to access these elements in order to use them to look up a value in a matrix A
I have a for loop that's iterating over the list:
list = [1,2,3,4,5]
for i in list:
value = A[i,i+1] #access A[1,2], A[2,3], A[3,4], A[4,5]
the problem with this is that I can't do an i+1 operation to access the n+1 element of my list. This is my first time programming in Python and I assumed element access would be the same as in C/C++ but it's not. Any help would be appreciated.
You can use slicing operator like this
A = [1, 2, 3, 4, 5]
for i in range(len(A) - 1):
value = A[i:i+2]
The range function lets you iterate len(A) - 1 times.
Enumerate can give you access to the index of each item:
for i, _ in enumerate(A[:-1]):
value = A[i:i+2]
If all you need are the pairs of data:
for value in zip(A, A[1:]):
value

Python index out of range in list slicing [duplicate]

This question already has answers here:
Why does substring slicing with index out of range work?
(3 answers)
Closed 9 years ago.
While this code will raise indexError:
In [1]: lst = [1, 2, 3]
In [2]: lst[3]
IndexError: list index out of range
Slicing the list with "out of range index" will not produce any error.
In [3]: lst[3:]
Out[3]: []
What is the rationale of this design?
When you are accessing an element in a list whose index is beyond its length, we cannot return anything. (There is no way we can represent an element which is not there). That's why the error is thrown. But when you are slicing, you are making a sliced COPY of the original list and that new list can be empty if the start or end are not valid.
It's nice being able to test if an element exists:
if sys.argv[2:]:
# do something with sys.argv[2], knowing it exists

Categories

Resources