[Tutor] remove blank list items

Kent Johnson kent37 at tds.net
Fri Sep 14 16:34:23 CEST 2007


Michael Langford wrote:
> First off:
>   What you want is a set, not a list. Lucky for you, a python dict uses 
> a set for its keys, so cheat and use that.

Python has had a set type in the stdlib since 2.3 and built-in since 2.4 
  so there is no need to cheat any more.

> change:
> for j in input:
>     print j,
> 
> to
> mySet={}
> for j in input:
>    mySet[j]=j

mySet = set()
for j in input:
   mySet.add(j)

or just
mySet = set(input)

> for item in mySet.keys():
>    print item

for item in mySet:
   print item

(actually the above works for dict or set, iterating a dict gives its keys)

Kent


More information about the Tutor mailing list