What is the difference between len() and size in Python?

92 views

What is the difference between len() and size in Python?

What is the difference between len() and size in Python?

solveurit24@gmail.com Changed status to publish February 13, 2025
0
  • len(): Returns the number of elements in an object (e.g., a list, string, or dictionary).
  • size (e.g., array.size in NumPy or sys.getsizeof()): Returns the memory size of an object.

Example:

import numpy as np

my_list = [1, 2, 3]
my_array = np.array(my_list)

print(len(my_list))  # Output: 3
print(my_array.size)  # Output: 3

solveurit24@gmail.com Changed status to publish February 13, 2025
0
You are viewing 1 out of 1 answers, click here to view all answers.