Do the items in a list have to be the same datatype?


Do the items in a list have to be the same datatype?


Items in a list aren’t forced to be of the same type by Python’s syntax rules, no. However! Lists typically do contain just a single data type, like all strings or all numbers or all booleans.
This allows us to easily perform operations on the entire list, which is sort of the point of maintaining a list data structure in the first place. If we had a number in our list of zoo_animals, which is otherwise all strings, it’d make it difficult to do a single operation of any kind over the entire list.
If we wanted to print the name of each animal, like we do in this exercise, and our last list item was the number 10 for some reason, it would give us an error when we tried printing it! We’d have to, as you may recall, convert it to a string using str() just to print it out like the others! Imagine the nightmare it would cause if we had tons of different data types in our list! We would have to account for every single case, rather than just having a general print functionality!


Please, I’m stuck with my lists, I have this:
person = name3

l = [["name1","Group1"], ["name2","Group2"], ["name3","Group3"], ["name4","Group4"]]

I would like to go through all items of my list l and check if person is inside it. If it inside, we append it in another list.
I tried a lot of methods but it check only if l[0] == person and it go out the condition



person = name3

why isn’t name3 a string?

as for your comparison, l[0] gives a list:

print(l[0]) # ["name1","Group1"]

a list won’t equal a string, you need to check if person is in l[0], l[1] and so forth


Yes. You’re right. I forgot that. Thanks.