3

I have a list which has elements of type int and str. I need to copy the int elements to another list. I tried a list comprehension which is not working. My equivalent loop is working.

input = ['a',1,'b','c',2,3,'c','d']
output = []
[output.append(a) for a in input if type(a) == int] 

[None, None, None]

same logic in loop works.

output = []
for a in input:
 if type(a) == int:
 output.append(a)
print(output)

[1,2,3,]

Can I know what made the difference.

asked Sep 12, 2018 at 1:03
3
  • 4
    Your logic does work, just print(output) at the end. Commented Sep 12, 2018 at 1:07
  • 1
    well your list comprehension never got any values, it just kept giving them to output, just write a if instead of 'output.append(a)' if you want them to go the list comprehensoion Commented Sep 12, 2018 at 1:08
  • Your original code works fine. Commented Sep 12, 2018 at 1:11

5 Answers 5

10

When you're doing:

input = ['a',1,'b','c',2,3,'c','d']
output = []
[output.append(a) for a in input if type(a) == int] 

append returns None, so that's why lot of none's

Btw,:

print(output)

Will be desired result.

Your logic actually works!

But it's not good to use list comprehension as side-effects, so best way would be:

output=[a for a in input if type(a) == int] 

Also final best would be isinstance:

output=[a for a in input if isinstance(a,int)] 
answered Sep 12, 2018 at 1:08
Sign up to request clarification or add additional context in comments.

Comments

3

An idiomatic solution would be:

 input = ['a', 1, 'b', 'c', 2, 3, 'c', 'd']
 output = [a for a in input if type(a) == int]

You do not want to use output.append. That is implied by the list comprehension. You can change the first a in the comprehension to an expression containing a. output.append(a) is a method call that returns NONE

Use list comprehensions when you want to collect values into a list you are assigning to a variable or as part of larger expression. Do not use them for side effects as a different format for a 'for loop'. Instead use the for loop.

Spaces after the commas are not required but are considered good style.

answered Sep 12, 2018 at 1:24

Comments

2
input = ['a',1,'b','c',2,3,'c','d']
output = [a for a in input if type(a) == int] 

The list comprehension automatically creates the list - no need to use append()

answered Sep 12, 2018 at 1:14

Comments

0

You can use the following solution to solve your problem:

output = [a for a in input if type(a) == int]
Grant Miller
29.2k16 gold badges158 silver badges171 bronze badges
answered Sep 12, 2018 at 1:15

Comments

0

A solution would be:

input = ['a',1,'b','c',2,3,'c','d']
output=[item for item in input if type(item) is int] or
output=[item for item in input if isinstance(item,int)]

and the last one is quicker than the first.

but when you use:

input = ['a',1,'b','c',2,3,'c','d']
output=[]
[output.append(a) for a in input if type(a) == int] 

it means the code execute two results ,the one is output.append(a) append object in end and the list comprehension with if condition creates a new list object after executing ,as a.append()(Look_in_IDLE) return None , so there are three None.

answered Sep 12, 2018 at 2:39

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.