Related
I have the following list:
listInitial = [20, 33, 24, 11, 0, 4, 3, 11]
I'm trying to write a function that iterates through it and returns a copy of this list with 11 removed. Desired output is: [20, 33, 24, 0, 4, 3]
I want to use the append method and for loop in my function. Below is my code:
listSecond = []
def iterateandremove (listInitial, num):
for i in listInitial:
if i != num:
listSecond.append(i)
print (listSecond)
return (listSecond)
iterateandremove (listInitial, 11)
When I run the code it prints the correct list out. But my function doesn't return anything at all. What am I doing wrong?
You should define listSecond in your function and return it from there.
This way if you run it multiple times, you only return your input list and not all the previous ones combined.
def iterateandremove (listInitial, num):
listSecond = []
for i in listInitial:
if i != num:
listSecond.append(i)
return (listSecond)
removed = iterateandremove(listInitial, 11)
print(removed)
#[20, 33, 24, 0, 4, 3]
You could also do this with a simple list comprehension without declaring listSecond
def iterateandremove(initial, num):
return [n for n in initial if n != num]
When I run the code it prints the correct list out. But my function doesn't return anything at all. What am I doing wrong?
That's because you didn't assign it to a variable. You'll also want to create a new container within the function, and not use a global variable. Try this:
def iterate_and_remove(list_initial, num):
list_second = []
for i in list_initial:
if i != num:
list_second.append(i)
return list_second
result = iterate_and_remove([1,2,3,4], 4)
print(result)
You could also do this with a list comprehension:
list_second = [i for i in list_initial if i != 11]
Another alternative;
listInitial = [20, 33, 24, 11, 0, 4, 3, 11]
def iterateandremove(listInitial, num):
return list(filter((num).__ne__, listInitial))
print(iterateandremove(listInitial, 11))
[20, 33, 24, 0, 4, 3]
lambda function also a good candidate for this;
listInitial = [20, 33, 24, 11, 0, 4, 3, 11]
def iterateandremove(listInitial, num):
return list(filter(lambda digit: digit != num, listInitial))
print(iterateandremove(listInitial, 11))
[20, 33, 24, 0, 4, 3]
Everything seems ok and the function is returning the list without 11.
returnedList = iterateandremove (listInitial, 11)
print(returnedList) #[20, 33, 24, 0, 4, 3]
def iterateandremove(listInitial, num):
return [n for n in listInitial if n != num]
listInitial = [20, 33, 24, 11, 0, 4, 3, 11]
print(iterateandremove(listInitial, 11))
If you are using python3, the list comprehension would do the work for you like the code above.
If you are using python2, I would suggest using python3 instead, for more details, see https://www.python.org/doc/sunset-python-2/
I've got this piece of code:
numbers = list(range(1, 50))
for i in numbers:
if i < 20:
numbers.remove(i)
print(numbers)
but the result I'm getting is:
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]
Of course, I'm expecting the numbers below 20 to not appear in the results. Looks like I'm doing something wrong with the remove.
You're modifying the list while you iterate over it. That means that the first time through the loop, i == 1, so 1 is removed from the list. Then the for loop goes to the second item in the list, which is not 2, but 3! Then that's removed from the list, and then the for loop goes on to the third item in the list, which is now 5. And so on. Perhaps it's easier to visualize like so, with a ^ pointing to the value of i:
[1, 2, 3, 4, 5, 6...]
^
That's the state of the list initially; then 1 is removed and the loop goes to the second item in the list:
[2, 3, 4, 5, 6...]
^
[2, 4, 5, 6...]
^
And so on.
There's no good way to alter a list's length while iterating over it. The best you can do is something like this:
numbers = [n for n in numbers if n >= 20]
or this, for in-place alteration (the thing in parens is a generator expression, which is implicitly converted into a tuple before slice-assignment):
numbers[:] = (n for in in numbers if n >= 20)
If you want to perform an operation on n before removing it, one trick you could try is this:
for i, n in enumerate(numbers):
if n < 20 :
print("do something")
numbers[i] = None
numbers = [n for n in numbers if n is not None]
Begin at the list's end and go backwards:
li = list(range(1, 15))
print(li)
for i in range(len(li) - 1, -1, -1):
if li[i] < 6:
del li[i]
print(li)
Result:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
[6, 7, 8, 9, 10, 11, 12, 13, 14]
#senderle's answer is the way to go!
Having said that to further illustrate even a bit more your problem, if you think about it, you will always want to remove the index 0 twenty times:
[1,2,3,4,5............50]
^
[2,3,4,5............50]
^
[3,4,5............50]
^
So you could actually go with something like this:
aList = list(range(50))
i = 0
while i < 20:
aList.pop(0)
i += 1
print(aList) #[21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]
I hope it helps.
The ones below are not bad practices AFAIK.
EDIT (Some more):
lis = range(50)
lis = lis[20:]
Will do the job also.
EDIT2 (I'm bored):
functional = filter(lambda x: x> 20, range(50))
So I found a solution but it's really clumsy...
First of all you make an index array, where you list all the index' you want to delete like in the following
numbers = range(1, 50)
index_arr = []
for i in range(len(numbers):
if numbers[i] < 20:
index_arr.append(i)
after that you want to delete all the entries from the numbers list with the index saved in the index_arr. The problem you will encounter is the same as before. Therefore you have to subtract 1 from every index in the index_arr after you just removed a number from the numbers arr, like in the following:
numbers = range(1, 50)
index_arr = []
for i in range(len(numbers):
if numbers[i] < 20:
index_arr.append(i)
for del_index in index_list:
numbers.pop(del_index)
#the nasty part
for i in range(len(index_list)):
index_list[i] -= 1
It will work, but I guess it's not the intended way to do it
As an additional information to #Senderle's answer, just for records, I thought it's helpful to visualize the logic behind the scene when python sees for on a "Sequence type".
Let's say we have :
lst = [1, 2, 3, 4, 5]
for i in lst:
print(i ** 2)
It is actually going to be :
index = 0
while True:
try:
i = lst.__getitem__(index)
except IndexError:
break
print(i ** 2)
index += 1
That's what it is, there is a try-catch mechanism that for has when we use it on a Sequence types or Iterables(It's a little different though - calling next() and StopIteration Exception).
*All I'm trying to say is, python will keep track of an independent variable here called index, so no matter what happens to the list (removing or adding), python increments that variable and calls __getitem__() method with "this variable" and asks for item.
Building on and simplying the answer by #eyquem ...
The problem is that elements are being yanked out from under you as you iterate, skipping numbers as you progress to what was the next number.
If you start from the end and go backwards, removing items on-the-go won't matter, because when it steps to the "next" item (actually the prior item), the deletion does not affect the first half of the list.
Simply adding reversed() to your iterator solves the problem. A comment would be good form to preclude future developers from "tidying up" your code and breaking it mysteriously.
for i in reversed(numbers): # `reversed` so removing doesn't foobar iteration
if i < 20:
numbers.remove(i)
You could also use continue to ignore the values less than 20
mylist = []
for i in range(51):
if i<20:
continue
else:
mylist.append(i)
print(mylist)
Since Python 3.3 you may use the list copy() method as the iterator:
numbers = list(range(1, 50))
for i in numbers.copy():
if i < 20:
numbers.remove(i)
print(numbers)
[20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]
Is there a way to slice through a whole list while excluding a range of values or multiple range of values in the middle of the list?
For example:
list = [1,2,3,4,5,6,7,8,9,0]
print list[......] #some code inside
I'd like the above code to print the list while excluding a range of values so the output would be: [1,2,3,8,9,0] or excluding multiple value ranges so the output would be: [1,2,6,7,0] by using the slice notation or any other simple method you can suggest.
Use list comprehensions:
>>> mylist = [1,2,3,4,5,6,7,8,9,0]
>>> print [i for i in mylist if i not in xrange(4,8)]
[1, 2, 3, 8, 9, 0]
Or if you want to exclude numbers in two different ranges:
>>> print [i for i in mylist if i not in xrange(4,8) and i not in xrange(1,3)]
[3, 8, 9, 0]
By the way, it's not good practice to name a list list. It is already a built-in function/type.
If the list was unordered and was a list of strings, you can use map() along with sorted():
>>> mylist = ["2", "5", "3", "9", "7", "8", "1", "6", "4"]
>>> print [i for i in sorted(map(int,mylist)) if i not in xrange(4,8)]
[1, 2, 3, 8, 9]
>>> nums = [1,2,3,4,5,6,7,8,9,0]
>>> exclude = set(range(4, 8))
>>> [n for n in nums if n not in exclude]
[1, 2, 3, 8, 9, 0]
Another example
>>> exclude = set(range(4, 8) + [1] + range(0, 2))
>>> [n for n in nums if n not in exclude]
[2, 3, 8, 9]
Using a method, and an exclude list
def method(l, exclude):
return [i for i in l if not any(i in x for x in exclude)]
r = method(range(100), [range(5,10), range(20,50)])
print r
>>>
[0, 1, 2, 3, 4, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
My example uses ranges with ints. But this method can be any list of items, with any number of exclude lists with other items, as long as the items have an equals comparison.
Edit:
A much faster method:
def method2(l, exclude):
'''
l is a list of items, exclude is a list of items, or a list of a list of items
exclude the items in exclude from the items in l and return them.
'''
if exclude and isinstance(exclude[0], (list, set)):
x = set()
map(x.add, [i for j in exclude for i in j])
else:
x = set(exclude)
return [i for i in l if i not in x]
Given my_list = [1,2,3,4,5,6,7,8,9,0], in one line, with enumerate() and range() (or xrange() in Python 2.x):
[n for i, n in enumerate(my_list) if i not in range(3, 7)]
I was wondering if using this was also valid:
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
print my_list[:3] + my_list[7:]
Here's a function that takes multiple slice objects and gives you a list containing only the items included by those slices. You can exclude items by specifying around what you want to exclude.
from itertools import chain
def sliceAndDice(sequence, *slices):
return list(chain(*[sequence[slice] for slice in slices]))
So, If you have the list [0,1,2,3,4,5,6,7,8,9] and you want to exclude the 4,5,6 in the middle, you could do this:
sliceAndDice([0,1,2,3,4,5,6,7,8,9], slice(0,4), slice(7,None))
This would return [0, 1, 2, 3, 7, 8, 9].
It works with lists of things that aren't numbers: sliceAndDice(['Amy','John','Matt','Joey','Melissa','Steve'], slice(0,2), slice(4,None)) will leave out 'Matt' and 'Joey', resulting in ['Amy', 'John', 'Melissa', 'Steve']
It won't work right if you pass in slices that are out of order or overlapping.
It also creates the whole list at once. A better (but more complicated) solution would be to create a iterator class that iterates over only the the items you wish to include. The solution here is good enough for relatively short lists.
This was my source I started with.
My List
L = [0, 23, 234, 89, None, 0, 35, 9]
When I run this :
L = filter(None, L)
I get this results
[23, 234, 89, 35, 9]
But this is not what I need, what I really need is :
[0, 23, 234, 89, 0, 35, 9]
Because I'm calculating percentile of the data and the 0 make a lot of difference.
How to remove the None value from a list without removing 0 value?
>>> L = [0, 23, 234, 89, None, 0, 35, 9]
>>> [x for x in L if x is not None]
[0, 23, 234, 89, 0, 35, 9]
Just for fun, here's how you can adapt filter to do this without using a lambda, (I wouldn't recommend this code - it's just for scientific purposes)
>>> from operator import is_not
>>> from functools import partial
>>> L = [0, 23, 234, 89, None, 0, 35, 9]
>>> list(filter(partial(is_not, None), L))
[0, 23, 234, 89, 0, 35, 9]
A list comprehension is likely the cleanest way:
>>> L = [0, 23, 234, 89, None, 0, 35, 9
>>> [x for x in L if x is not None]
[0, 23, 234, 89, 0, 35, 9]
There is also a functional programming approach but it is more involved:
>>> from operator import is_not
>>> from functools import partial
>>> L = [0, 23, 234, 89, None, 0, 35, 9]
>>> list(filter(partial(is_not, None), L))
[0, 23, 234, 89, 0, 35, 9]
Using list comprehension this can be done as follows:
l = [i for i in my_list if i is not None]
The value of l is:
[0, 23, 234, 89, 0, 35, 9]
For Python 2.7 (See Raymond's answer, for Python 3 equivalent):
Wanting to know whether something "is not None" is so common in python (and other OO languages), that in my Common.py (which I import to each module with "from Common import *"), I include these lines:
def exists(it):
return (it is not None)
Then to remove None elements from a list, simply do:
filter(exists, L)
I find this easier to read, than the corresponding list comprehension (which Raymond shows, as his Python 2 version).
#jamylak answer is quite nice, however if you don't want to import a couple of modules just to do this simple task, write your own lambda in-place:
>>> L = [0, 23, 234, 89, None, 0, 35, 9]
>>> filter(lambda v: v is not None, L)
[0, 23, 234, 89, 0, 35, 9]
Iteration vs Space, usage could be an issue. In different situations profiling may show either to be "faster" and/or "less memory" intensive.
# first
>>> L = [0, 23, 234, 89, None, 0, 35, 9, ...]
>>> [x for x in L if x is not None]
[0, 23, 234, 89, 0, 35, 9, ...]
# second
>>> L = [0, 23, 234, 89, None, 0, 35, 9]
>>> for i in range(L.count(None)): L.remove(None)
[0, 23, 234, 89, 0, 35, 9, ...]
The first approach (as also suggested by #jamylak, #Raymond Hettinger, and #Dipto) creates a duplicate list in memory, which could be costly of memory for a large list with few None entries.
The second approach goes through the list once, and then again each time until a None is reached. This could be less memory intensive, and the list will get smaller as it goes. The decrease in list size could have a speed up for lots of None entries in the front, but the worst case would be if lots of None entries were in the back.
The second approach would likely always be slower than the first approach. That does not make it an invalid consideration.
Parallelization and in-place techniques are other approaches, but each have their own complications in Python. Knowing the data and the runtime use-cases, as well profiling the program are where to start for intensive operations or large data.
Choosing either approach will probably not matter in common situations. It becomes more of a preference of notation. In fact, in those uncommon circumstances, numpy (example if L is numpy.array: L = L[L != numpy.array(None) (from here)) or cython may be worthwhile alternatives instead of attempting to micromanage Python optimizations.
Say the list is like below
iterator = [None, 1, 2, 0, '', None, False, {}, (), []]
This will return only those items whose bool(item) is True
print filter(lambda item: item, iterator)
# [1, 2]
This is equivalent to
print [item for item in iterator if item]
To just filter None:
print filter(lambda item: item is not None, iterator)
# [1, 2, 0, '', False, {}, (), []]
Equivalent to:
print [item for item in iterator if item is not None]
To get all the items that evaluate to False
print filter(lambda item: not item, iterator)
# Will print [None, '', 0, None, False, {}, (), []]
from operator import is_not
from functools import partial
filter_null = partial(filter, partial(is_not, None))
# A test case
L = [1, None, 2, None, 3]
L = list(filter_null(L))
If it is all a list of lists, you could modify sir #Raymond's answer
L = [ [None], [123], [None], [151] ]
no_none_val = list(filter(None.__ne__, [x[0] for x in L] ) )
for python 2 however
no_none_val = [x[0] for x in L if x[0] is not None]
""" Both returns [123, 151]"""
<< list_indice[0] for variable in List if variable is not None >>
L = [0, 23, 234, 89, None, 0, 35, 9]
result = list(filter(lambda x: x is not None, L))
If the list has NoneType and pandas._libs.missing.NAType objects than use:
[i for i in lst if pd.notnull(i)]
I've got this piece of code:
numbers = list(range(1, 50))
for i in numbers:
if i < 20:
numbers.remove(i)
print(numbers)
but the result I'm getting is:
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]
Of course, I'm expecting the numbers below 20 to not appear in the results. Looks like I'm doing something wrong with the remove.
You're modifying the list while you iterate over it. That means that the first time through the loop, i == 1, so 1 is removed from the list. Then the for loop goes to the second item in the list, which is not 2, but 3! Then that's removed from the list, and then the for loop goes on to the third item in the list, which is now 5. And so on. Perhaps it's easier to visualize like so, with a ^ pointing to the value of i:
[1, 2, 3, 4, 5, 6...]
^
That's the state of the list initially; then 1 is removed and the loop goes to the second item in the list:
[2, 3, 4, 5, 6...]
^
[2, 4, 5, 6...]
^
And so on.
There's no good way to alter a list's length while iterating over it. The best you can do is something like this:
numbers = [n for n in numbers if n >= 20]
or this, for in-place alteration (the thing in parens is a generator expression, which is implicitly converted into a tuple before slice-assignment):
numbers[:] = (n for in in numbers if n >= 20)
If you want to perform an operation on n before removing it, one trick you could try is this:
for i, n in enumerate(numbers):
if n < 20 :
print("do something")
numbers[i] = None
numbers = [n for n in numbers if n is not None]
Begin at the list's end and go backwards:
li = list(range(1, 15))
print(li)
for i in range(len(li) - 1, -1, -1):
if li[i] < 6:
del li[i]
print(li)
Result:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
[6, 7, 8, 9, 10, 11, 12, 13, 14]
#senderle's answer is the way to go!
Having said that to further illustrate even a bit more your problem, if you think about it, you will always want to remove the index 0 twenty times:
[1,2,3,4,5............50]
^
[2,3,4,5............50]
^
[3,4,5............50]
^
So you could actually go with something like this:
aList = list(range(50))
i = 0
while i < 20:
aList.pop(0)
i += 1
print(aList) #[21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]
I hope it helps.
The ones below are not bad practices AFAIK.
EDIT (Some more):
lis = range(50)
lis = lis[20:]
Will do the job also.
EDIT2 (I'm bored):
functional = filter(lambda x: x> 20, range(50))
So I found a solution but it's really clumsy...
First of all you make an index array, where you list all the index' you want to delete like in the following
numbers = range(1, 50)
index_arr = []
for i in range(len(numbers):
if numbers[i] < 20:
index_arr.append(i)
after that you want to delete all the entries from the numbers list with the index saved in the index_arr. The problem you will encounter is the same as before. Therefore you have to subtract 1 from every index in the index_arr after you just removed a number from the numbers arr, like in the following:
numbers = range(1, 50)
index_arr = []
for i in range(len(numbers):
if numbers[i] < 20:
index_arr.append(i)
for del_index in index_list:
numbers.pop(del_index)
#the nasty part
for i in range(len(index_list)):
index_list[i] -= 1
It will work, but I guess it's not the intended way to do it
As an additional information to #Senderle's answer, just for records, I thought it's helpful to visualize the logic behind the scene when python sees for on a "Sequence type".
Let's say we have :
lst = [1, 2, 3, 4, 5]
for i in lst:
print(i ** 2)
It is actually going to be :
index = 0
while True:
try:
i = lst.__getitem__(index)
except IndexError:
break
print(i ** 2)
index += 1
That's what it is, there is a try-catch mechanism that for has when we use it on a Sequence types or Iterables(It's a little different though - calling next() and StopIteration Exception).
*All I'm trying to say is, python will keep track of an independent variable here called index, so no matter what happens to the list (removing or adding), python increments that variable and calls __getitem__() method with "this variable" and asks for item.
Building on and simplying the answer by #eyquem ...
The problem is that elements are being yanked out from under you as you iterate, skipping numbers as you progress to what was the next number.
If you start from the end and go backwards, removing items on-the-go won't matter, because when it steps to the "next" item (actually the prior item), the deletion does not affect the first half of the list.
Simply adding reversed() to your iterator solves the problem. A comment would be good form to preclude future developers from "tidying up" your code and breaking it mysteriously.
for i in reversed(numbers): # `reversed` so removing doesn't foobar iteration
if i < 20:
numbers.remove(i)
You could also use continue to ignore the values less than 20
mylist = []
for i in range(51):
if i<20:
continue
else:
mylist.append(i)
print(mylist)
Since Python 3.3 you may use the list copy() method as the iterator:
numbers = list(range(1, 50))
for i in numbers.copy():
if i < 20:
numbers.remove(i)
print(numbers)
[20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]