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!

FAQ: Learn Python: Functions - Introduction to Lists