I am trying to implement a list comprehension for a for loop-nested if code block. However, what seems to work using the conventional nested form does not seem to work in its list comprehension representation. From what I could see after spending a considerable amount of time is that it follows the logic as required. Please let me know if there is anything else I am skipping.
for x in data_list:
if x not in encoding:
encoding.append(x)
Using list comprehension
encoding = [x for x in data_list if x not in encoding]
Thank you.
Your issue is here:
encoding = [x for x in data_list if x not in encoding]
What you are doing is you are reassigning encoding to this list comprehension [x for x in data_list if x not in encoding].
So what you're doing is setting encoding to be only the elements that weren't in it in the first place.
What you should do is this:
encoding.extend([x for x in data_list if x not in encoding])
In this way, you're extending the list with the results of the list comprehension.
Here's some test code:
encoding = ['a','b','c','d']
encoding2 = ['a','b','c','d']
data_list = ['a','b','c','d','d','d','e','f','g']
print(encoding)
print(encoding2)
for x in data_list:
if x not in encoding:
encoding.append(x)
encoding2.extend([x for x in data_list if x not in encoding2])
print(encoding)
print(encoding2)
which prints:
['a', 'b', 'c', 'd']
['a', 'b', 'c', 'd']
['a', 'b', 'c', 'd', 'e', 'f', 'g']
['a', 'b', 'c', 'd', 'e', 'f', 'g']
Now, this is not a perfect solution, as it will still duplicate elements if they're in data_list more than once. The reason for this is that in the for loop example, encoding is checked after each append operation whereas the list comprehension operates only based on encoding's initial state. So it will push elements into it more than once if they're in data_list more than once.
If you want to get around this, convert the list comprehension to a set first as follows:
encoding.extend(set([x for x in data_list if x not in encoding]))
There you have it!
Your two examples are not equivalent. If you want to append elements from data_list to encoding that are not already in encoding (which is what your for-loop example does), but using list-comprehension, do:
encoding.extend(x for x in data_list if x not in encoding)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With