I have the following program. I am trying to understand list comprehension and set comprehension:
mylist = [i for i in range(1,10)]
print(mylist)
clist = []
for i in mylist:
if i % 2 == 0:
clist.append(i)
clist2 = [x for x in mylist if (x%2 == 0)]
print('clist {} clist2 {}'.format(clist,clist2))
#set comprehension
word_list = ['apple','banana','mango','cucumber','doll']
myset = set()
for word in word_list:
myset.add(word[0])
myset2 = {word[0] for word in word_list}
print('myset {} myset2 {}'.format(myset,myset2))
My question is why the curly braces for myset2 = {word[0] for word in word_list}
.
I haven't come across sets in detail before.