How to get length of a list of lists in python
Asked Answered
R

5

18

So, if I have a list called myList I use len(myList) to find the number of elements in that list. Fine. But how do I find the number of lists in a list?

text = open("filetest.txt", "r")
myLines = text.readlines()
numLines=len(myLines)
print numLines

The above text file used has 3 lines of 4 elements separated by commas. The variable numLines prints out as '4' not '3'. So, len(myLines) is returning the number of elements in each list not the length of the list of lists.

When I print myLines[0] I get the first list, myLines[1] the second list, etc. But len(myLines) does not show me the number of lists, which should be the same as 'number of lines'.

I need to determine how many lines are being read from the file.

Romaromagna answered 22/11, 2013 at 17:41 Comment(6)
In your code, you have a list of lines (strings). not a list of lists. And python will happily count empty lines... Did you check to make sure you don't have any empty lines?Fusee
len(myLines) will give you the number of objects inside that list no matter if they are lists, dicts or strings.Delrosario
Your code works absolutely fine. Give us the file as is.Vikkivikky
Just print myLines and the reason will be clear.Tapia
Python will not automatically split text read from a file on commas. Use the csv module if you want that to happen.Teador
If the text file used has 3 lines, len(myLines) will be 3 regardless of the content of each. Perhaps you have a blank line at the end of the file. Another thing to try is using open("filetest.txt", "rt") because some platforms have multiple characters at the end of each line of a text file which could confuse readlines().Trafficator
K
25

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]
Kasper answered 22/11, 2013 at 19:7 Comment(0)
E
4

You can do it with functools.reduce:

from functools import reduce 

a = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [], [1, 2]]
print(reduce(lambda count, l: count + len(l), a, 0))
# result is 11
Encyclopedist answered 1/7, 2019 at 7:41 Comment(0)
M
1

"The above text file used has 3 lines of 4 elements separated by commas. The variable numLines prints out as '4' not '3'. So, len(myLines) is returning the number of elements in each list not the length of the list of lists."

It sounds like you're reading in a .csv with 3 rows and 4 columns. If this is the case, you can find the number of rows and lines by using the .split() method:

text = open("filetest.txt", "r").read()
myRows = text.split("\n")      #this method tells Python to split your filetest object each time it encounters a line break 
print len(myRows)              #will tell you how many rows you have
for row in myRows:
  myColumns = row.split(",")   #this method will consider each of your rows one at a time. For each of those rows, it will split that row each time it encounters a comma.  
  print len(myColumns)         #will tell you, for each of your rows, how many columns that row contains
Mcclary answered 22/11, 2013 at 18:5 Comment(0)
C
-2

if the name of your list is listlen then just type len(listlen). This will return the size of your list in the python.

Crispin answered 10/1, 2015 at 20:31 Comment(0)
C
-2

The method len() returns the number of elements in the list.

 list1, list2 = [123, 'xyz', 'zara'], [456, 'abc']
    print "First list length : ", len(list1)
    print "Second list length : ", len(list2)

When we run above program, it produces the following result −

First list length : 3 Second list length : 2

Crispin answered 11/2, 2018 at 17:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.