0
Why [0]
skills = {'Python', 'HTML', 'SQL', 'C++', 'Java', 'Scala'} job_skills = {'HTML', 'CSS', 'JS', 'C#', 'NodeJS'} print(list(skills & job_skills)[0])
2 Answers
+ 8
Abubakarsiddiq Shah
the expected output of this exercise is 'HTML' (without the quotes)
but the result of the set intersection operation will be a list like: ['HTML']
by using [0] we select the first element in the resulting list.
>>> BUT: this will only work correctly if just 1 element is the result of the set operation. if the intersection gives MORE than one result (LIKE: C++ and HTML), you may use:
...
print(*list(skills & job_skills), sep=', ') # C++, HTML
or:
print(*list(skills & job_skills)) # C++ HTML
using ...[0] with more than one result will output only the first element of the list like `C++`.
+ 6
Because you are getting in list so if you just print that you will get result as list instead of value.We need value
#Why [0]
skills = {'Python', 'HTML', 'SQL', 'C++', 'Java', 'Scala'}
job_skills = {'HTML', 'CSS', 'JS', 'C#', 'NodeJS'}
print(list(skills & job_skills))
print(list(skills & job_skills)[0])