Python Forum
Exercise about list of lists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Exercise about list of lists
#1
Hi,

I'd like to know if this is the correct way to approach this kind of problem.
Basically I need to store the sum of every list of L in a new list.
For example: [[1, 2, 3], [5, 6]] => [6, 11]

#!/usr/bin/python3

L = [[1,2,3],[5,6],[6,7]] 
a = []

for i in range(0, len(L)):
    sum = 0
    for k in L[i]:
        sum += k
    a.append(sum)
print (a)
Reply
#2
That looks correct to me, but isn't how I'd solve the problem. Here's the feedback I'd give, where some of it doesn't apply once later stuff is done but I include it anyway:
1) Rather than using an index in your outer loop, you can iterate over the lists directly just like you iterate over the inner values directly in the nested loop. So you don't need range() at all.
2) You're overwriting a built-in name, "sum". On my screen, the forum renders that code as pink to indicate that it's a built-in word. Instead, I'd call it "total" or...
3) You can replace the inner loop with a call to the built-in sum function.
4) I'd not use append(), instead I would just use a list comprehension.
5) It's often best to put things like this in a well-named function.
Reply
#3
Thanks for your answer, I think I almost got every point of your post (I'll write the function but I know how to do that so it's not an issue):

#!/usr/bin/python3

L = [[1,2,3],[5,6]] 

a = [sum(i) for i in L]
    
print (a)
Did you mean to do something like this?
Reply
#4
Yeah, that looks good. I'd probably call it "sub" or "collection" or "ints" or something instead of just "i" though (and L I'd probably call "lists" or something). While that's probably more Pythonic, you could also skip doing the naming -
a = list(map(sum, L))
If you only really care about producing an iterable (rather than a list) you can drop the call to list there as well. (In Python 3, map creates a generator rather than a list.)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  List all possibilities of a nested-list by flattened lists sparkt 1 929 Feb-23-2023, 02:21 PM
Last Post: sparkt
  user input values into list of lists tauros73 3 1,078 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  returning a List of Lists nafshar 3 1,082 Oct-28-2022, 06:28 PM
Last Post: deanhystad
  Creating list of lists, with objects from lists sgrinderud 7 1,676 Oct-01-2022, 07:15 PM
Last Post: Skaperen
  How to efficiently average same entries of lists in a list xquad 5 2,145 Dec-17-2021, 04:44 PM
Last Post: xquad
  sorting a list of lists by an element leapcfm 3 1,896 Sep-10-2021, 03:33 PM
Last Post: leapcfm
  behavior list of lists roym 5 2,116 Jul-04-2021, 04:43 PM
Last Post: roym
  List of lists - merge sublists with common elements medatib531 1 3,419 May-09-2021, 07:49 AM
Last Post: Gribouillis
  Sort List of Lists by Column Nju 1 11,621 Apr-13-2021, 11:59 PM
Last Post: bowlofred
  Exercise List Overlap Damian 7 3,175 Apr-02-2021, 07:39 AM
Last Post: Damian

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020