How do I get the number of elements in a list (length of a list) in Python?
To get the number of elements in a list in Python, you can use the
Here's an example:
my_list = [1, 2, 3, 4] num_elements = len(my_list) print(num_elements) # Output: 4
Watch a video course Python - The Practical Guide
You can also use the
len() function to get the length of other types of sequences, such as strings and tuples.
my_string = "hello" string_length = len(my_string) print(string_length) # Output: 5 my_tuple = (1, 2, 3) tuple_length = len(my_tuple) print(tuple_length) # Output: 3