10 Highly Probable Data Scientist Interview Questions



Original Source Here

6. What is the difference between lists and sets

Let’s do an example to demonstrate the main difference between lists and sets.

mylist = list(text)
myset = set(text)
print(mylist)
['P', 'y', 't', 'h', 'o', 'n', ' ', 'i', 's', ' ', 'a', 'w', 'e', 's', 'o', 'm', 'e', '!']
print(myset)
{'t', ' ', 'i', 'e', 'm', 'P', '!', 'y', 'o', 'h', 'n', 'a', 's', 'w'}

As we notice in the resulting objects, the list contains all the characters in the string whereas the set only contains unique values.

Another difference is that the characters in the list are ordered based on their location in the string. However, there is order associated with the characters in the set.

AI/ML

Trending AI/ML Article Identified & Digested via Granola by Ramsey Elbasheer; a Machine-Driven RSS Bot

%d bloggers like this: