This question already has answers here:
list comprehension with multiple conditions (python)
(2 answers)
Closed 6 years ago.
In my code, I'm trying to loop through an enumeration object and selectively add some values from the enumeration to a new list using a list comprehension.
This works:
a = [1, 2, 3, 4, 5]
s = [i[1] for i in enumerate(a)]
Of course, that basically just copies list a over to s.
This doesn't work (a being the same list):
s = [i[1] if i[1] != 2 for i in enumerate(a)]
I would think that this would just copy every element of list a over to s besides the 2, but instead I get a syntax error. Anybody know what's going on here?
You misplaced the if part:
s = [i[1] for i in enumerate(a) if i[1] != 2]
Related
This question already has answers here:
Strange result when removing item from a list while iterating over it
(8 answers)
How to remove items from a list while iterating?
(25 answers)
Closed 4 years ago.
I'm trying to iterate over a list of number and removing the values that are lower than a number that I use to compare.
My problem is that there's a number that is lower than the value that I use but it doesnt get removed.
I'm using the remove() function of the list but I don't know why it doesn't get removed
Here is my code:
def remove_lower_numbers(array_numbers, bigger_number):
for elem in array_numbers:
if elem <= bigger_number:
array_numbers.remove(elem)
print(array_numbers)
It works if I used a list comprehension like this:
array_numbers = [x for x in array_numbers if x >= bigger_number]
but I want to do it the way I firts mentioned for learning purposes
I call the function like this:
cards_array = [3, 2, 7]
remove_lower_numbers(cards_array, 8)
but the function prints:
[2]
and 2 is lower than 8 it should return None or a empty list.
Using filter, which keeps only the values that return True for the lambda function:
list(filter(lambda x: x > 3, [1, 2, 3, 4, 5, 2, 3]))
Output:
[4, 5]
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]
This question already has answers here:
if else in a list comprehension [duplicate]
(8 answers)
What does "list comprehension" and similar mean? How does it work and how can I use it?
(5 answers)
Closed 6 years ago.
I want to make a list comprehension with duplicated values at one loop cycle like this:
s=[]
for i in range(5):
s+=[i,i]
But this doesnt work.
[i, i for i in range(5)]
You can do it like this:
[i for i in range(5) for j in range(2)]
The output is:
[0, 0, 1, 1, 2, 2, 3, 3, 4, 4]
The i loop provides the values, and the j loop serves to repeat those values.
Here is one way with zip:
[i for p in zip(range(5), range(5)) for i in p]
There's no direct way to use a single level list comprehension to do exactly the same thing you're doing with your explicit loop. That's because each iteration of the loop in a comprehension must produce at most one value (it can produce zero if you have an if clause).
However, for this specific case, you can use division to produce the sequence of repeated values you want while iterating on a larger range:
s = [i//2 for i in range(10)]
I think this will do the trick.
[ i/2 for i in range(10)]
List comprehension will have as many elements as the iterator within the comprehension. In this case, the iterator is range(10)
Since, you need 10 elements. You will need to range over 10 and apply some computation( in this case i/2) to get the desired element.
You may try this:
s = [[i, i] for i in range(5)].
And if you want it to be flattened you can try this nested list comprehension.
s = [x for y in [[i, i] for i in range(5)] for x in y]
This question already has answers here:
How to remove items from a list while iterating?
(25 answers)
Closed 6 years ago.
I am tryin to delete an item from a list by specifying the index of the list I want to delete but I get an error.
My code:
tuu = [1,2,3,4,'nan', 8]
for i in range(len(tuu)):
if tuu[i] == 'nan':
del tuu[i]
but I get the error:
7 for i in range(len(tuu)):
----> 8 if tuu[i] == 'nan':
9 del tuu[i]
IndexError: list index out of range
When you use a for loop, Python will call __iter__ on the iterable you are looping over. __iter__ will return an iterator object which keeps track of where you are currently in your iterable.
If you modify the length of your iterable (the list) while looping over it, the iterator will get confused.
After deleting tuu[i] your list has one less element, but the iterator won't know and tries to access the previously last index -> that's where you get an IndexError.
The canonical way to filter a list in Python is to build a new list and reassign the name of the old list:
>>> tuu = [1, 2, 3, 4, 'nan', 8]
>>> tuu = [x for x in tuu if x != 'nan']
>>> tuu
[1, 2, 3, 4, 8]
This question already has answers here:
Efficient way to rotate a list in python
(27 answers)
Closed 9 years ago.
Say you have a list [1,2,3,4]
And I want to get [2,3,4,1] or [3,4,1,2].
Basically I am using the list with a different starting point each time, but then continuing through the list. How would I create something to recognize that in python.
What i have now is list[n:] where n is the shifted value, say 2, making you start at three.
someList[n:] + someList[:n]
would solve your purpose if n <= len(someList)
Also, collections.deque is the efficient way.
I believe this is what you want
>>> def startAt(index, list):
... print list[index:] + list[:index]
...
>>> l = [0,1,2,3,4,5]
>>> startAt(3, l)
[3, 4, 5, 0, 1, 2]
>>>