Removing elements from a List in Python - python

I'm using python 3.4 and just learning the basics, so please bear with me..
listA = [1,2]
for a in listA:
listA.remove(a)
print(listA)
What is suppose is I get an empty list, but what I get is a list with value '2'. I debugged the code with large no. of values in list and when the list is having a single element the for loop exit.
Why is the last element not removed from the list..?

You should not change a list while iterating over it. The indices of the list change as you remove items, so that some items are never evaluated. Use a list comprehension instead, which creates a new list:
[a for a in list if ...]
In other words, try something like this:
>>> A = [1, 2, 3, 4]
>>> A = [a for a in A if a < 4] # creates new list and evaluates each element of old
>>> A
[1, 2, 3]
When you use a for-loop, an internal counter is used. If you shift the remaining elements to the left while iterating over the list, the left-most element in the remaining list will be not be evaluated. See the note for the for statement.

That happens because the length of the for is evaluated only at the beginning and you modify the list while looping on it:
>>> l = [1,2,3]
>>> l
[1, 2, 3]
>>> for a in l:
print(a)
print(l)
l.remove(a)
print(a)
print(l)
print("---")
1
[1, 2, 3]
1
[2, 3]
---
3
[2, 3]
3
[2]
---
>>>
See? The value of the implicit variable used to index the list and loop over it increases and skip the second element.
If you want to empty a list, do a clear:
>>> l.clear()
>>> l
[]
Or use a different way of looping over the list, if you need to modify it while looping over it.

As mentioned by #Justin in comments, do not alter the list while iterating on it. As you keep on removing the elements from the list, the size of the list shrinks, which will change the indices of the element.
If you need to remove elements from the list one-by-one, iterate over a copy of the list leaving the original list intact, while modifying the duplicated list in the process.
>>> listA = [1,2,3,4]
>>> listB = [1,2,3,4]
>>> for each in listA:
... print each
... listB.remove(each)
1
2
3
4
>>> listB
[]

Related

In python i have to remove all the same occurance element using remove() in list data type

i have wrote a code for deleting same occurrence element remove from list using #remove method
l=[1,1,1,2,2,2,2,3,3]
x=int(input("enter the element given in the list:"))#when input is 2
for i in l:
if i==x:
l.remove(i)
print(l)
but o/p: is coming [1, 1, 1, 2, 2, 3, 3]
but all 2 should remove but not removing
Don't remove elements while iterating over a list, the result could be unexpected.
Instead, try assign a new list using list comprehension:
l = [x for x in l if x != 2]
Or better, use tools like filterfalse.

Add part of the list to another list in Python [duplicate]

This question already has answers here:
How do I concatenate two lists in Python?
(31 answers)
Closed 2 months ago.
I am trying to understand if it makes sense to take the content of a list and append it to another list.
I have the first list created through a loop function, that will get specific lines out of a file and will save them in a list.
Then a second list is used to save these lines, and start a new cycle over another file.
My idea was to get the list once that the for cycle is done, dump it into the second list, then start a new cycle, dump the content of the first list again into the second but appending it, so the second list will be the sum of all the smaller list files created in my loop. The list has to be appended only if certain conditions met.
It looks like something similar to this:
# This is done for each log in my directory, i have a loop running
for logs in mydir:
for line in mylog:
#...if the conditions are met
list1.append(line)
for item in list1:
if "string" in item: #if somewhere in the list1 i have a match for a string
list2.append(list1) # append every line in list1 to list2
del list1 [:] # delete the content of the list1
break
else:
del list1 [:] # delete the list content and start all over
Does this makes sense or should I go for a different route?
I need something efficient that would not take up too many cycles, since the list of logs is long and each text file is pretty big; so I thought that the lists would fit the purpose.
You probably want
list2.extend(list1)
instead of
list2.append(list1)
Here's the difference:
>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> c = [7, 8, 9]
>>> b.append(a)
>>> b
[4, 5, 6, [1, 2, 3]]
>>> c.extend(a)
>>> c
[7, 8, 9, 1, 2, 3]
Since list.extend() accepts an arbitrary iterable, you can also replace
for line in mylog:
list1.append(line)
by
list1.extend(mylog)
To recap on the previous answers. If you have a list with [0,1,2] and another one with [3,4,5] and you want to merge them, so it becomes [0,1,2,3,4,5], you can either use chaining or extending and should know the differences to use it wisely for your needs.
Extending a list
Using the list classes extend method, you can do a copy of the elements from one list onto another. However this will cause extra memory usage, which should be fine in most cases, but might cause problems if you want to be memory efficient.
a = [0,1,2]
b = [3,4,5]
a.extend(b)
>>[0,1,2,3,4,5]
Chaining a list
Contrary you can use itertools.chain to wire many lists, which will return a so called iterator that can be used to iterate over the lists. This is more memory efficient as it is not copying elements over but just pointing to the next list.
import itertools
a = [0,1,2]
b = [3,4,5]
c = itertools.chain(a, b)
Make an iterator that returns elements from the first iterable until it is exhausted, then proceeds to the next iterable, until all of the iterables are exhausted. Used for treating consecutive sequences as a single sequence.
Take a look at itertools.chain for a fast way to treat many small lists as a single big list (or at least as a single big iterable) without copying the smaller lists:
>>> import itertools
>>> p = ['a', 'b', 'c']
>>> q = ['d', 'e', 'f']
>>> r = ['g', 'h', 'i']
>>> for x in itertools.chain(p, q, r):
print x.upper()
You can also combine two lists (say a,b) using the '+' operator.
For example,
a = [1,2,3,4]
b = [4,5,6,7]
c = a + b
Output:
>>> c
[1, 2, 3, 4, 4, 5, 6, 7]
That seems fairly reasonable for what you're trying to do.
A slightly shorter version which leans on Python to do more of the heavy lifting might be:
for logs in mydir:
for line in mylog:
#...if the conditions are met
list1.append(line)
if any(True for line in list1 if "string" in line):
list2.extend(list1)
del list1
....
The (True for line in list1 if "string" in line) iterates over list and emits True whenever a match is found. any() uses short-circuit evaluation to return True as soon as the first True element is found. list2.extend() appends the contents of list1 to the end.
You can simply concatnate two lists, e.g:
list1 = [0, 1]
list2 = [2, 3]
list3 = list1 + list2
print(list3)
>> [0, 1, 2, 3]
Using the map() and reduce() built-in functions
def file_to_list(file):
#stuff to parse file to a list
return list
files = [...list of files...]
L = map(file_to_list, files)
flat_L = reduce(lambda x,y:x+y, L)
Minimal "for looping" and elegant coding pattern :)
you can use __add__ Magic method:
a = [1,2,3]
b = [4,5,6]
c = a.__add__(b)
Output:
>>> c
[1,2,3,4,5,6]
If we have list like below:
list = [2,2,3,4]
two ways to copy it into another list.
1.
x = [list] # x =[] x.append(list) same
print("length is {}".format(len(x)))
for i in x:
print(i)
length is 1
[2, 2, 3, 4]
2.
x = [l for l in list]
print("length is {}".format(len(x)))
for i in x:
print(i)
length is 4
2
2
3
4

How to loop over a list of lists

Recently I have come up with using a for loop to reform a list.
First, I did this:
list1 = [[1],[2],[3]]
list2 = []
for x in list1:
list2.append(x)
print(list2)
And the output was:
[[1], [2], [3]]
Then, I tried:
list2 = []
for x in list1:
list2.append(x[0])
And the output was:
[1, 2, 3]
Could someone please explain to me what x[0] does in this for loop? I thought this index would mean only taking the first element in a list.
x[0] returns the item at index 0 of the list x, so by appending to a new list with x[0] in your for loop, you are appending the value within the sub-list rather than the sub-list itself, thereby achieving the effect you want, flattening the list.
Could you please tell what does "x[0]" do in this for loop; I thought this index would mean only taking the first element in a list.
list1 = [[1],[2],[3]] is not simply a list but a list of lists. Every element of list1 is a list – albeit with only one sub-element. So you are effectively iterating over every list within your list list1, picking the first element at position 0 (which is all there is) and appending it to your new list list2.
When you go
for x in list1:
and print out x you'll get:
>[1]
>[2]
>[3]
Because each element in list1 are still lists! The lists only have one element but they're still stored within a list. When you use x[0] you're telling Python you want to access the first element of each list, which is the number you want.
Also another thing you could do which would be faster than a for loop is to use list comprehension:
list2 = [x[0] for x in list1]
list1 = [[1], [2], [3]]
for x in list1:
list2.append(x[0])
Okay lets just look at what we are doing here:
for x in list1:
Well x in this loop will represent [1], [2], [3] which are list containing a certain number in the 0 position.
A little more perspective lets look at it like this:
list1[0][0] = 1
list1[1][0] = 2
list1[2][0] = 3
You are iterating list containing list.
e.g :
a = [[1], [2], [3]]
a[0] will contain list [1] #i.e it is containing list
a[1] will contain list [2]
a[2] will contain list [3]
If you are want to access contents from list of list, you have to pass 2 indexes.
e.g :
a[0][0] will contain 1 #i.e it is containing element
a[1][0] will contain 2
a[2][0] will contain 3
You also can try extend keyword to achieve the same. The extend() method takes a single argument (a list) and adds it to the end.
list1 = [[1],[2],[3]]
list2 = []
for x in list1:
list2.extend(x)
print(list2)

Dynamic self-referencing conditional in list comprehension

Goal: Create a conditional statement in a list comprehension that (1) dynamically tests -- i.e., upon each iteration -- if the element is not in the list being comprehended given (2) the list is itself updated on each iteration.
Background code:
arr = [2, 2, 4]
l = list()
Desired output:
l = [2, 4]
Desired behavior via for loop:
for element in arr:
if element not in l:
l.append(element)
Incorrect list comprehension not generating desired behavior:
l = [element for element in arr if element not in l]
Question restated: How do I fix the list comprehension so that it generates the desired behavior, i.e., the desired output stated above?
If you absolutely must use a list comprehesion, you can just recast your for loop into one. The downside is that you will end up with a list of None elements, since that is what list.append returns:
>>> arr = [2, 2, 4]
>>> l = list()
>>> _ = [l.append(element) for element in arr if element not in l]
>>> print(l)
[2, 4]
>>> print(_)
[None, None]
If you are tied to comprehensions, but not necessarily to list comprehensions, you can use the generator comprehension suggested by #tdelaney. This will not create any unwanted byproducts and will do exactly what you want.
>>> arr = [2, 2, 4]
>>> l = list()
>>> l.extend(element for element in arr if element not in l)
A better way than either would probably be to put the original list into a set and then back into a list. The advantage of using a set to extending a list is that sets are much faster at adding elements after checking for prior containment. A list has to do a linear search and reallocate every time you add an element.
>>> l = list(set(arr))
if you want to remove duplicates why not use set(the list containing duplicates) or list(dict.fromkeys(the list containing duplicates)?
but to answer your Question:
i think the whole thing is just wrong, l (your list) doesn't get updated with each iteration since it's inside the list comprehension

how to safely remove elements from a list in Python

I loop through a list and remove the elements that satisfy my condition. But why doesn't this work, as noted below? Thank you.
>>> a=[ i for i in range(4)]
>>> a
[0, 1, 2, 3]
>>> for e in a:
... if (e > 1) and (e < 4):
... a.remove(e)
...
>>> a
[0, 1, 3]
>>> a=[ i for i in range(4)]
>>> for e in a:
... if (e > -1) and (e < 3):
... a.remove(e)
...
>>> a
[1, 3]
You cannot change something while you're iterating it. The results are weird and counter-intuitive, and nearly never what you want. In fact, many collections explicitly disallow this (e.g. sets and dicts).
Instead, iterate over a copy (for e in a[:]: ...) or, instead of modifying an existing list, filter it to get a new list containing the items you want ([e for e in a if ...]). Note that in many cases, you don't have to iterate again to filter, just merge the filtering with the generation of the data.
Why don't you just do this initially in the list comprehension? E.g.
[i for i in range(4) if i <= 1 or i >= 4]
You can also use this to construct a new list from the existing list, e.g.
[x for x in a if x <= 1 or x >= 4]
The idea of filtering is a good one, however it misses the point which is that some lists may be very large and the number of elements to remove may be very small.
In which case the answer is to remember the list indexes of the elements to remove and then iterate through the list of indexes, sorted from largest to smallest, removing the elements.
The easiest way to visualize it is to think of the iteration working on list-offsets instead of the actual items - do something to the first item, then the second item, then the third item, until it runs out of items. If you change the number of items in the list, it changes the offsets of all the remaining items in the list:
lst = [1,2,3,4]
for item in lst:
if item==2:
lst.remove(item)
else:
print item
print lst
results in
1
4
[1,3,4]
which makes sense if you step through it like so:
[1,2,3,4]
^
first item is not 2, so print it -> 1
[1,2,3,4]
^
second item is 2, so remove it
[1,3,4]
^
third item is 4, so print it -> 4
The only real solution is do not change the number of items in the list while you are iterating over it. Copy the items you want to keep to a new list, or keep track of the values you want to remove and do the remove-by-value in a separate pass.
It is not safe to remove elements from a list while iterating though it. For that exists the filter function. It takes a function(that admits one argument) and an iterable(in this case your list). It returns a new iterable of the same type(list again here) with the elements where the function applied to that element returned True:
In your case you can use a lambda function like this:
a = filter(lambda x: x > 1 and x < 4, range(4))
or if you have the list already:
a = range(4)
a = filter(lambda x: x > 1 and x < 4, a)
remember that if you are using python3 it will return an iterator and not a list.

Categories

Resources