I'm a beginner in Python and trying my hand at using Leetcode & other online resources to learn Python! Recently saw this solution being passed around for longestCommonPrefix on leetcode (thank you hwendy12 !) and was a little curious as how zip(*list) works.
def longestCommonPrefix(self, strs):
"""
:type strs: List[str]
:rtype: str
"""
answer = ''
for i in zip(*strs):
if len(set(i)) == 1:
answer += i[0]
else:
break
return answer
I tried searching around and it seems that the * 'unpacks' a list and makes each element a separate argument? I'm a little shaky on how zip works as well and so just wanted to ask what the difference between what zip(list) vs zip(*list) does and what each produces.
Thank you so much!