sharp
sharp

Reputation: 2158

Python - How many are just a single word in a list

How do I get total number of single words in a list. There are 8 elements in a list below. However, some elements have more than single words in them 'apples are delicious', how to do I just capture just the number of single word from list?

list = ['apples', 'apples', 'apples are delicious', 'oranges', 'fruits', 'kiwi', 'fruit festivals', 'festivals']

len(list) # total number of elements in a list
list.count("apples") # only counts a specific word but not a element single words

# total 8 elements 
# how many are only 1 word?
# results: 6/8 are 1 words

Upvotes: 1

Views: 578

Answers (7)

Thematrixme
Thematrixme

Reputation: 318

Most of the answers here are indeed correct.

However you may not want to rely only on whitespaces to define whether a string is multiwords or not. I mean apple/orange is two words but will not be tokenized as such if you just check whether there are whitespaces inside. You might want to use \w+ in regex inside.

Agreeing also with other comments about split() creating a list per entry (and also having to loop over the whole entry string), as well as len implicitly creating a list of all the results as well, here is my proposition:

import re
single_word = re.compile(r'^\w+$')

lst = ['apples', 'apples', 'apples are delicious', 'oranges', 'fruits', 'kiwi', 'fruit festivals', 'festivals']
print(sum(1 for w in lst if re.match(single_word, w)))

>>>> 6

This way you loop over the list once and sum on the fly. And per entry you loop over it only once as well and stop as soon as a non-word character is found. This is probably one of the fastest simple solutions.

Upvotes: 0

RahulPalve
RahulPalve

Reputation: 1

Here is simple logical way to do the same:

count=0
for word in l:
  if ' ' in word:
    count=count+1
print(count)

Here I simply using in statement to check spaces.

Upvotes: 0

qqqqq
qqqqq

Reputation: 923

X = ['apples', 'apples', 'apples are delicious', 'oranges', 'fruits', 'kiwi', 'fruit festivals', 'festivals']
ar = [i for i in X if len(i.split()) == 1]
print(len(ar))

Upvotes: 1

Sushant
Sushant

Reputation: 3669

Python's list comprehension -

fruits = ['apples', 'apples', 'apples are delicious', 'oranges', 'fruits', 'kiwi', 'fruit festivals', 'festivals']
result1 = len([i for in in fruits if len(i.split('')) == 1])
result2 = len([i for in in fruits if ' ' not in i])

Can be done with regex also -

import re
result3 = len([i for in in fruits if not re.match('\s', i)])

Upvotes: 0

Jean-François Fabre
Jean-François Fabre

Reputation: 140168

The fastest way would be:

count = sum(1 for x in lst if ' ' not in x)
  • it doesn't use split so it doesn't create a new list
  • it doesn't create a list just to apply len on it

it just adds 1 every time the generator comprehension finds that space isn't in the word

Upvotes: 3

Sunitha
Sunitha

Reputation: 12015

>>> lst = ['apples', 'apples', 'apples are delicious', 'oranges', 'fruits', 'kiwi', 'fruit festivals', 'festivals']
>>> len([w for w in lst if ' ' not in w])
6

By the way, avoid name list for variables as list is a builtin

Upvotes: 6

Andrej Kesely
Andrej Kesely

Reputation: 195438

Using filter function:

l = ['apples', 'apples', 'apples are delicious', 'oranges', 'fruits', 'kiwi', 'fruit festivals', 'festivals']

count = len(list(filter(lambda v: len(v.split()) == 1, l)))

print(count)

Output:

6

Upvotes: 2

Related Questions