views:

56

answers:

2

I want to simply add some word to a list and then count how many words are in there...

And check if the word isn't in the list already.

How can I do it?

+2  A: 

You can add a word to a list by calling alist.append("word") where alist is your list.

To count how many words are in the list simply use len(alist).

To check if the word isn't already in the list use if "word" not in alist:

-edit to remove references to word 'list', replacing it with 'alist'

Justin Hamilton
Don't name your list `list`.
Tim Pietzcker
The obvious python primitive to use in this case is a set, not a list
jsbueno
+4  A: 

A list can be used for this, but if speed is important and order doesn't matter then a set will be faster.

>>> S = set(['a', 'b', 'c'])
>>> S
set(['a', 'c', 'b'])
>>> 'b' in S
True
>>> S.add('d')
>>> S
set(['a', 'c', 'b', 'd'])
>>> S.add('b')
>>> S
set(['a', 'c', 'b', 'd'])
Ignacio Vazquez-Abrams
make an example please, speed is impostqant and order doesn't matter
Shady
Shady, that is the example..
Tim McNamara
he edited =p ...
Shady