Python Forum
calculation with list in list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
calculation with list in list
#1
hello. with this code i can calculate 1 + 2 + 3 = 6
l = [1,2,3,]
def suml(l):
  s= 0
  for i in l:
    s = s + i
  return s  


but what can i do if i want to calculate this list ( 1 + 2 +3 +( 1 + 2) = 9)
l = [1,2,3,[1,2]]
Reply
#2
What have you thought about, or tried?
Reply
#3
Somehow I have to use the def inside the def but I cant solve it . Or call the def repeatedly
Reply
#4
You can try to flatten the nested lists first. There is a topic here about that.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
Well I need the code to work for both lists. It has to calculate every list that you can give .
Reply
#6
(Aug-08-2019, 06:43 PM)gianniskampanakis Wrote: Somehow I have to use the def inside the def but I cant solve it . Or call the def repeatedly

Do you have to use Recursion ?
Reply
#7
No recursion is not needed. I study this exercise in order to give exams at university. Theoretically we have not learn yet recursion

The help note says: Write a sumL function that will calculate the sum of the elements of a list of numbers and call this function repeatedly.]
Reply
#8
Γεια σου γιαννη






l = [1,2,3,[1,2]]

def suml(l, s=0):
    for i in l:
        if type(i) == int:
            s +=i 
        else:
            return suml(i, s)
    return s
l = [1,2,3,[1,2]]

def suml(l):
    s = 0 
    for i in l:
        if type(i) == int:
            s +=i 
        else:
            for t in i:
                s +=t      
    return s
Reply
#9
This looks like recursion
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#10
Σε ευχαριστώ το type(i) εκτός από int τι τιμες μπορεί να πάρει στην περίπτωση μας ;
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,091 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,466 May-01-2023, 09:06 PM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 878 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Сheck if an element from a list is in another list that contains a namedtuple elnk 8 1,714 Oct-26-2022, 04:03 PM
Last Post: deanhystad
Question Keyword to build list from list of objects? pfdjhfuys 3 1,500 Aug-06-2022, 11:39 PM
Last Post: Pedroski55
  Split a number to list and list sum must be number sunny9495 5 2,197 Apr-28-2022, 09:32 AM
Last Post: Dexty
  How to check if a list is in another list finndude 4 1,793 Jan-17-2022, 05:04 PM
Last Post: bowlofred
  Different out when using conda list and pip list Led_Zeppelin 1 3,966 Jan-14-2022, 09:30 PM
Last Post: snippsat
  Use one list as search key for another list with sublist of list jc4d 4 2,107 Jan-11-2022, 12:10 PM
Last Post: jc4d
  Need to parse a list of boolean columns inside a list and return true values Python84 4 2,036 Jan-09-2022, 02:39 AM
Last Post: Python84

Forum Jump:

User Panel Messages

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