


If a list within a list contains more than one element, they aren't taken into consideration. Note that the empty list counts as one element. Which prints: Number of elements in the list of lists: 6 Print( "Number of elements in the list of lists: ", number_of_elements) If we use the built-in function len(), the lists count as single elements, so we will have:ĭownload the eBook number_of_elements = len(list_e) However, lists can have, in turn, lists as their elements. In the introduction, we saw that elements of lists can be of different data types. We can see that list_d has a total of 8 elements, among which 5 are unique. Which prints: Number of elements in the list: 8 Print( "Number of unique elements in the list: ", number_of_unique_elements) Print( "Number of elements in the list: ", number_of_elements) Number_of_unique_elements = len( set(list_d)) We then pass that into the len() function to get the number of elements in the set: list_d = This function creates a set object, which rejects all duplicate values. If we want to get the number of elements without duplicates (unique elements) we can use another built-in function set(). Lists can have multiple elements, including duplicates. Additionally, you might want to perform some operation either on the elements themselves or an operation in general, which is possible here. This is a much more verbose solution compared to the len() function, but it is worth going through it as we will see later in the article that the same idea can be applied when we're dealing with a list of lists.
