This saves the data in a list of lists.

text = open("filetest.txt", "r")
data = [ ]
for line in text:
    data.append( line.strip().split() )

print "number of lines ", len(data)
print "number of columns ", len(data[0])

print "element in first row column two ", data[0][1]
Answer from Javier Castellanos on Stack Overflow
🌐
DataCamp
datacamp.com › tutorial › python-list-size
Python List Size: 8 Different Methods for Finding the Length of a List in Python | DataCamp
February 7, 2024 - The len() function is the most straightforward approach to ascertain the size of a list. It is not only concise but also highly efficient, making it the go-to method in most cases.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-ways-to-find-length-of-list
How To Find the Length of a List in Python - GeeksforGeeks
May 2, 2025 - len() function is a built-in Python function and it's the simplest and most efficient way to find the length of a list in Python.
Discussions

How to determine the size of all combined elements in a list of lists
This will loop through everything once, but it'll handle any depth (up to recursion limits): from collections.abc import Sequence def len_items(items): """Return the length of all nested iterables.""" return sum( len_items(item) if isinstance(item, Sequence) else 1 for item in items ) More on reddit.com
🌐 r/learnpython
26
70
December 31, 2020
How do I get the number of elements in a list (length of a list) in Python? - Stack Overflow
Lists and other similar builtin objects with a "size" in Python, in particular, have an attribute called ob_size, where the number of elements in the object is cached. So checking the number of objects in a list is very fast. But if you're checking if list size is zero or not, don't use len - instead, put the list in a boolean context - it is treated as False if empty, and True if non-empty. ... Return the length ... More on stackoverflow.com
🌐 stackoverflow.com
How to Find All Subsequences of a String?
def all_subs(word: str) -> list: substring = [] for idx in range(len(word)): string_portion = word[:idx + 1] # <-- w, wo, wor, word #string_portion = word[idx:] # <-- word, ord, rd, d substring.append(string_portion) print(string_portion) return substring word[from:to] = slice of the original word, from and to being numerical values between 0 (zero) and the length of the word - 1. If from = 0, it can be omitted word[:to], python assumes that its zero If to = length, it can be omitted word[from:] Hope it helps More on reddit.com
🌐 r/learnpython
4
3
October 25, 2018
[Python] How do I slice and print half of a string regardless of how long it is?
string[0:len(string)//2] More on reddit.com
🌐 r/learnprogramming
4
1
September 3, 2018
🌐
DigitalOcean
digitalocean.com › community › tutorials › find-the-length-of-a-list-in-python
How to find the length of a list in Python | DigitalOcean
July 25, 2025 - How to find the length of a list in Python using the len() function. Includes examples, edge cases, and tips for working with nested or dynamic lists.
🌐
IONOS
ionos.com › digital guide › websites › web development › python list length
How to find the length of a Python list - IONOS
July 18, 2023 - You can use the len or length_hint function to find out the length of a Python list.
🌐
Edureka
edureka.co › blog › python-list-length
How to Get the Length of List in Python? | Edureka
November 27, 2024 - The len() method is one of the easiest ways to find the length of list in Python. This is the most conventional technique adopted by all programmers.
Find elsewhere
🌐
Cherry Servers
cherryservers.com › home › blog › cloud computing › how to get the length of a list in python
How to Get the Length of a List in Python | Cherry Servers
November 7, 2025 - This tutorial has walked you through five different methods on how to find the length of a list in Python: len(), for loop, length_hint(), __len__() special function and the NumPy library.
🌐
freeCodeCamp
freecodecamp.org › news › python-list-length-how-to-get-the-size-of-a-list-in-python
Python List Length – How to Get the Size of a List in Python
March 3, 2022 - The code snippet below shows how to use the len() function to get the length of a list: demoList = ["Python", 1, "JavaScript", True, "HTML", "CSS", 22] sizeOfDemoList = len(demoList) print("The length of the list using the len() method is: " ...
🌐
Carmatec
carmatec.com › home › finding the length of a list in python with examples
Finding the Length of a List in Python With Examples
December 31, 2024 - In this article, we’ll explore the len() function in detail, along with examples and alternative approaches for measuring list length. The len() function is a built-in Python function that returns the number of items in an object.
🌐
iO Flood
ioflood.com › blog › python-length-of-list
Finding the Length of a List in Python (With Examples)
August 21, 2024 - Then, we used the len() function, which returned the number of elements in the list, which is 5. The len() function is a straightforward and efficient way to find the length of a list in Python.
🌐
W3Schools
w3schools.com › python › gloss_python_list_length.asp
Python List Length
Python DSA Lists and Arrays Stacks Queues Linked Lists Hash Tables Trees Binary Trees Binary Search Trees AVL Trees Graphs Linear Search Binary Search Bubble Sort Selection Sort Insertion Sort Quick Sort Counting Sort Radix Sort Merge Sort
🌐
Hostman
hostman.com › tutorials › how to get the length of a list in python
How to Find the Length of a List in Python: Quick Guide | Hostman
July 17, 2025 - Country_list = ["The United States ... length in Python with a for loop. The idea is to traverse the entire list while incrementing a counter by 1 on each iteration....
Price   $
Address   1999 Harrison St 1800 9079, 94612, Oakland
🌐
Note.nkmk.me
note.nkmk.me › home › python
Get the Size (Length, Number of Items) of a List in Python | note.nkmk.me
August 24, 2023 - In Python, you can get the size (length, number of items) of a list using the built-in len() function. Built-in Functions - len() — Python 3.11.3 documentation Get the size of a list with len() Get t ...
🌐
Great Learning
mygreatlearning.com › blog › it/software development › how to find length of list in python
How to Find Length of List in Python
June 27, 2025 - Using a loop (less common but illustrates concepts): You can count elements by iterating through the list. Let’s look at each one. The len() function is a built-in Python function. It takes an object as an argument and returns its length (the ...
🌐
Replit
replit.com › home › discover › how to find the length of a list in python
How to find the length of a list in Python | Replit
February 6, 2026 - The underscore, _, is a common Python convention. It’s used as a placeholder variable when you need to loop but don't need to use the value of each item. After the loop finishes, count holds the total number of elements. This manual approach is more verbose and less efficient than using len(), but it’s a great way to understand the mechanics of iteration. my_list = [10, 20, 30, 40, 50] length = sum(1 for _ in my_list) print(length)--OUTPUT--5
Top answer
1 of 11
2991

The len() function can be used with several different types in Python - both built-in types and library types. For example:

>>> len([1, 2, 3])
3
2 of 11
323

How do I get the length of a list?

To find the number of elements in a list, use the builtin function len:

items = []
items.append("apple")
items.append("orange")
items.append("banana")

And now:

len(items)

returns 3.

Explanation

Everything in Python is an object, including lists. All objects have a header of some sort in the C implementation.

Lists and other similar builtin objects with a "size" in Python, in particular, have an attribute called ob_size, where the number of elements in the object is cached. So checking the number of objects in a list is very fast.

But if you're checking if list size is zero or not, don't use len - instead, put the list in a boolean context - it is treated as False if empty, and True if non-empty.

From the docs

len(s)

Return the length (the number of items) of an object. The argument may be a sequence (such as a string, bytes, tuple, list, or range) or a collection (such as a dictionary, set, or frozen set).

len is implemented with __len__, from the data model docs:

object.__len__(self)

Called to implement the built-in function len(). Should return the length of the object, an integer >= 0. Also, an object that doesn’t define a __nonzero__() [in Python 2 or __bool__() in Python 3] method and whose __len__() method returns zero is considered to be false in a Boolean context.

And we can also see that __len__ is a method of lists:

items.__len__()

returns 3.

Builtin types you can get the len (length) of

And in fact we see we can get this information for all of the described types:

>>> all(hasattr(cls, '__len__') for cls in (str, bytes, tuple, list, 
                                            range, dict, set, frozenset))
True

Do not use len to test for an empty or nonempty list

To test for a specific length, of course, simply test for equality:

if len(items) == required_length:
    ...

But there's a special case for testing for a zero length list or the inverse. In that case, do not test for equality.

Also, do not do:

if len(items): 
    ...

Instead, simply do:

if items:     # Then we have some items, not empty!
    ...

or

if not items: # Then we have an empty list!
    ...

I explain why here but in short, if items or if not items is more readable and performant than other alternatives.

🌐
GeeksforGeeks
geeksforgeeks.org › python › find-size-of-a-ist-in-python
How to Find Length of a list in Python - GeeksforGeeks
July 11, 2025 - The length of a list means the number of elements it contains. In-Built len() function can be used to find the length of an object by passing the object within the parentheses. Here is the Python example to find the length of a list using len().
🌐
Python Examples
pythonexamples.org › python-list-length
Python List Length - len(list)
Python List Length Example - To get the length of list in Python, you can call the Python builtin function len() with the list passed as argument. len() returns an integer representing the number of elements in the list.