Week 1 assignment 2 : list(set(data))

hi guys just revisited some of the assignments for a deeper understanding and i realised that the list and set function in the dinosaur assignment was able to create chars where it has the characters in the list but when i tried to u the function to see how it works apparently it does something very different. does anyone knows why this might be the case?
`

The input data is different in the two cases. In one case it consists of words and in the other case it consists of letters. Try printing what data is before the second call. Have you tried googling “python set” to understand more about how it works.

Watch this:

words = ['abcde', 'efghij']
print(type(words))
print(len(words))
<class 'list'>
2

So that is a python iterable with 2 elements, both of which are unique.

Now try a similar thing with the names file:

data = open('dinos.txt', 'r').read()
print(type(data))
print(len(data))
print(data[0:100])
<class 'str'>
19909
Aachenosaurus
Aardonyx
Abdallahsaurus
Abelisaurus
Abrictosaurus
Abrosaurus
Abydosaurus
Acanthopholis

So you can see that data is a python iterable with 19909 elements, each of which is a character.

1 Like

yes i have, i was reading from this link Python set().


oh so something like this? thats kinda cool, is it just a property of the string object?

The article on “set” should have covered that. The input to “set” is an “iterable”. The data type “str” and “list” and “tuple” are all examples of python iterables. If you print the type of word in your new example, it is a “str”, right? The point is that set iterates over the iterable and only retains the unique entries, whatever they are. In a string, it gives you each character one at a time when you iterate over it. If you give it a general list type, it iterates over the elements of the list, whatever those are.