Why [0] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why [0]

skills = {'Python', 'HTML', 'SQL', 'C++', 'Java', 'Scala'} job_skills = {'HTML', 'CSS', 'JS', 'C#', 'NodeJS'} print(list(skills & job_skills)[0])

4th Sep 2023, 3:33 PM
Abubakarsiddiq Shah
Abubakarsiddiq Shah - avatar
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++`.
4th Sep 2023, 3:44 PM
Lothar
Lothar - avatar
+ 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])
4th Sep 2023, 3:36 PM
A͢J
A͢J - avatar