Using .remove in a loop in Python [duplicate] - python

This question already has answers here:
How to remove items from a list while iterating?
(25 answers)
Closed 2 years ago.
I tried the following code:
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
for x in arr:
if x > 3:
arr.remove(x)
print(arr)
It gives the following output:
[1, 2, 3, 5, 7, 9]
While instead i thought it would remove from the array every element bigger than 3. Why is that? And how can i make it remove every element bigger than a certain amount?

Modifying a list that you're in the process of iterating over will frequently yield unexpected results. A better option is to create a new list:
arr = [x for x in arr if x <= 3]

Related

get the second element that I search in a list [duplicate]

This question already has answers here:
How to find the index of the nth time an item appears in a list?
(7 answers)
Closed 3 years ago.
I use python and I have a list of numbers and I would like to search a number X but I must find the second one element and if it doesn't exist I must get a msg
Example:
list: [5, 6, 8, 7, 9, 6, 7, 9, 2, 4, 6, 8]
X = 6
return: i = 5
Ps: I need the faster code because I need to repeat it in a loop of 200000 i
A call to index searches through the list in order until it finds a match, and stops there. If you expect to need indices of more matches, you should use a list comprehension, or generator expression.
>>> [1, 1].index(1)
0
>>> [i for i, e in enumerate([1, 2, 1]) if e == 1]
[0, 2]
And then you get the second element of the result array

Strange output while modifying a list inside a for loop over the list [duplicate]

This question already has answers here:
How to remove items from a list while iterating?
(25 answers)
Closed 3 years ago.
H=[1,2,3,4,5,6,7,8]
for h in H:
print h
if h%2==1:
H.remove(h)
print H
Output:
1
[2, 3, 4, 5, 6, 7, 8]
3
[2, 4, 5, 6, 7, 8]
5
[2, 4, 6, 7, 8]
7
[2, 4, 6, 8]
So, I was just experimenting with the posibility of modifying a list inside a for loop over the same list, and while the list itself behaves like I thought, I don't get why the numbers 2,4,6,8 aren't being printed. Can anyone explain?
This is because you are modifying the list as your iterating through it. For ex, in your first iteration you are removing the element 1 and in your next iteration the index is 2. But in your modified list the element in index position 2 is 3 because you removed element 1.

list sort not working on part of a list [duplicate]

This question already has answers here:
Sort a part of a list in place
(3 answers)
Closed 4 years ago.
I am trying to sort part of a list.
My list is
A=[3,2,8,1,0,5,4,6,7,9]
I am able to sort the entire list by A.sort()
A.sort() -> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
When I apply sort() on part of the list, it doesn't work as expected. My list remains the same.
A[0:4].sort()-> [3,2,8,1,0,5,4,6,7,9]
I know sorted() will work as expected when I apply on part of a list but I would like to know why sort() does't work on part of the list.
Try:
A=[3,2,8,1,0,5,4,6,7,9]
A[0:4] = sorted(A[0:4])
print(A)
Output:
[1, 2, 3, 8, 0, 5, 4, 6, 7, 9]

Converting a 1D list into a 2D list with a given row length in python [duplicate]

This question already has answers here:
How do I split a list into equally-sized chunks?
(66 answers)
Closed 8 years ago.
Is there an easy way to convert a 1D list into a 2D list with a given row length?
Suppose I have a list like this:
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9]
I want to convert the above list into a 3 x 3 table like below:
myList = [[1,2,3],[4,5,6],[7,8,9]]
I know I can accomplish this by creating a new 2D list called myList2 and inserting the element into MyList2 using 2 nested for loops. Like MyList2[x][y] = myList[i] i++
I think there should be a better way to do it (without using external libraries or modules)
Thanks!
Using list comprehension with slice:
>>> myList = [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> n = 3
>>> [myList[i:i+n] for i in range(0, len(myList), n)]
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Why does "for item in list" loop only go half way? [duplicate]

This question already has answers here:
How to remove items from a list while iterating?
(25 answers)
Modifying list while iterating [duplicate]
(7 answers)
Closed 8 years ago.
I'm trying to modify a loop function that contains over a hundred lines of code.
If I have a loop in python that goes through a list, why does a for item in list: loop only finish half way through? Shouldn't it keep looping until all items are popped?
For example if I have this list:
l1 = [1,2,3,4,5,6]
for item in l1:
l1.pop(0)
print l1
No matter how many items the list contains, it will only finish when 50% of the list has been popped:
OUTPUT FOR CODE ABOVE:
[2, 3, 4, 5, 6]
[3, 4, 5, 6]
[4, 5, 6]
Process finished with exit code 0
Can I still pop each item in the list without changing the for item in l1: loop?
You could create a copy of the list and pop from the original one:
In [1]: l1 = [1,2,3,4,5,6]
In [2]: for item in l1[:]:
...: l1.pop(0)
...: print l1
...:
[2, 3, 4, 5, 6]
[3, 4, 5, 6]
[4, 5, 6]
[5, 6]
[6]
[]
However, this is certainly not elegant.

Categories

Resources