0

At the moment, I'm just testing this out and will add it to my program once I understand it. I'd just like to know how to find the length of a list inside a list.

I've tried:

l = [["1","2","3"],["1","2","3","4"],["1","2","3","4","5"]]
[print(len(x)) for x in l[0]]

But it just prints 1 three times. I just need it to print (or return) the lengths of each list inside l.

zondo
  • 19,901
  • 8
  • 44
  • 83
Gabby Freeland
  • 1,527
  • 2
  • 12
  • 10

2 Answers2

5

You have a typo: you wrote l[0] where l would have sufficed.

for x in l:
    print(len(x))

Note that it is bad practice to have side effects in a list comprehension — list comprehensions are to create values, not to change global state. You should use a for loop.

Fengyang Wang
  • 11,901
  • 2
  • 38
  • 67
  • What side effects are there in the OP's list comp? It's not changing global state at all - it's simply a more succinct and more Pythonic way of creating a `for` loop. – MattDMo Sep 19 '16 at 23:56
  • `print` is a side effect. Using list comprehensions to perform side effects and then throwing away the value is certainly not Pythonic. Please see [this question](http://stackoverflow.com/questions/5753597/is-it-pythonic-to-use-list-comprehensions-for-just-side-effects). – Fengyang Wang Sep 19 '16 at 23:59
  • @MattDMo It's function isn't represented by it's immediate return (which is always `null`), so anything it does must effect a global state (the output stream in this case). – Carcigenicate Sep 20 '16 at 00:23
  • I'm still not convinced. It's not like we're programming in Lisp or F# here, it's Python. Besides, we're going to be calling `print()` anyways, so why not do it in a more concise manner? Oh, and BTW @Carcigenicate, it technically should be `None`, not `null` :) – MattDMo Sep 20 '16 at 00:53
  • @MattDMo Feng was talking technically. It doesn't make as much sense to use a comprehension since we don't care about the return type, because the action we're carrying out operates purely by side effects. A for-loop makes more sense since it doesn't result in a useless list of `None`s (my bad). Why map when you don't care about the return? – Carcigenicate Sep 20 '16 at 01:03
  • @Carcigenicate the list is never assigned to anything, so it is garbage-collected as soon as it's filled. Yes, that might take a few more bytes of memory than a for loop, but this is Python on a desktop/laptop, not assembly on an embedded chip. Yes, it *is* extraneous, and yes, you *are* technically wasting resources, but this is a high-level language. I just tested, and there is no time difference between the for loop and the list comp (I was printing to `os.devnull` so there were no worries about screen lag). – MattDMo Sep 20 '16 at 01:21
  • 1
    @MattDMo It's more a better of best practice. I wouldn't be surprised if they were both optimized into the same code since the result is never used. A comprehension shows the intent to map. A for-loop is used to do effects over a list. It was a little pedantic of Feng to include it in the answer, but I agree with his point. – Carcigenicate Sep 20 '16 at 01:27
  • *A matter of best practice. I don't know how I missed that typo. – Carcigenicate Sep 20 '16 at 21:30
0
for x in l:
   print(len(x))

It print:

3
4
5
Jisson
  • 51
  • 7