Reputation: 1
I'm new in python and I got confused. Can anyone please explain to me the difference of the two variants of making a set from a list? Which one is more correct?
a = ["Jake", "John", "Eric"]
b = ["John", "Jill"]
c = set([])
d = set([])
for i in range (len(a)):
c.add(a[i])
for y in range (len(b)):
d.add(b[y])
print c.difference(d)
import sets
e= sets.Set(a)
print e
f = sets.Set(b)
print f
print e.difference(f)
Outcome
set(['Jake', 'Eric'])
Set(['Jake', 'Eric'])
Thanx!
Upvotes: 0
Views: 72
Reputation: 97301
It doesn't take a for loop to convert a list to set:
a = ["Jake", "John", "Eric"]
b = ["John", "Jill"]
print set(a) - set(b)
use set
object instead of sets.Set()
, sets.Set()
is deprecated.
Upvotes: 1