caasswa
caasswa

Reputation: 521

How to add to this list comprehension? Python

Still getting used to using python and wondering, if I wanted to have a count for the number of x which are NOT files, how can I change this line?

teeth = [{"tooth_path": str(x)} for x in p if os.path.isfile(x)]

I still want teeth returning what it currently is, i just want a count with how many are not files.

Upvotes: 0

Views: 67

Answers (2)

Guinther Kovalski
Guinther Kovalski

Reputation: 1909

p = os.listdir()    
sum([not os.path.isfile(f) for f in p])

Upvotes: 0

Barmar
Barmar

Reputation: 780889

Simple subtraction:

not_teeth_count = len(p) - len(teeth)

Upvotes: 6

Related Questions