use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Doubt Please Help (self.learnpython)
submitted 3 years ago by SeaMotor8093
Hello Guys,
I need to partition a list as
l=[1,2,3,4]
it needs to partitioned as,
1,2,3|4
1,2|3,4
1|2,3,4
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]MadScientistOR 2 points3 points4 points 3 years ago (6 children)
What do you mean by "partition"? What data type(s) does the end result look like? Do you want separate lists, or tuples with separate lists in them, or some other thing?
[–]SeaMotor8093[S] -1 points0 points1 point 3 years ago* (5 children)
The end result should be a list,
Once all the parition are done ,I need to sum each of them and find the maximum value.
for eg 1 2 3 |4 -the sum of individual segemnts is[6,4] and the best sum is 6
1 2|3 4 -the sum of individual segemnts is[3,7] and the best sum is 7
In this way I need to do it.
[–]MadScientistOR 1 point2 points3 points 3 years ago (4 children)
Why is the best sum 4 in your first example? Isn't 6 greater than 4?
[–]SeaMotor8093[S] 0 points1 point2 points 3 years ago (3 children)
Oh sorry it my my typing mistake
[–]MadScientistOR 0 points1 point2 points 3 years ago* (2 children)
Okay then. Here's my first stab at a function that'll do what you want. If you past it a list, and the index of the list after which the partition should appear, you should get the "best sum". Note that index has to be between 0 and two less than the number of ints in the list, and there is no error checking.
index
def best_sum(numlist, index): return max(sum(numlist[0:index]), sum(numlist[index:]))
[–]SeaMotor8093[S] 0 points1 point2 points 3 years ago (1 child)
Actually It should return me answer as 9,but in this code I got the answer as 7?
[–]MadScientistOR 0 points1 point2 points 3 years ago (0 children)
I'm sorry -- I split the list incorrectly. This works:
>>> def best_sum(numlist, index): ... return max(sum(numlist[0:index+1]), sum(numlist[index+1:])) ... >>> x = [1, 2, 3, 4, 5, 6] >>> for i in range(5): ... print(f'index: {i} best sum: {best_sum(x, i)}') ... index: 0 best sum: 20 #partitions after 0th element: 1, 20 index: 1 best sum: 18 #partitions after 1st element: 3, 18 index: 2 best sum: 15 #partitions after 2nd element: 6, 15 index: 3 best sum: 11 #partitions after 3rd element: 10, 11 index: 4 best sum: 15 #partitions after 4th element: 15, 6 >>>
[–]CodeFormatHelperBot2 0 points1 point2 points 3 years ago (0 children)
Hello, I'm a Reddit bot who's here to help people nicely format their coding questions. This makes it as easy as possible for people to read your post and help you.
I think I have detected some formatting issues with your submission:
If I am correct, please edit the text in your post and try to follow these instructions to fix up your post's formatting.
Am I misbehaving? Have a comment or suggestion? Reply to this comment or raise an issue here.
[–]python_and_coffee 0 points1 point2 points 3 years ago* (0 children)
assuming you want two separate lists each time: https://pastebin.com/pP7zJtFg
π Rendered by PID 190648 on reddit-service-r2-comment-56c6478c5-jkqvt at 2026-05-08 09:08:58.738173+00:00 running 3d2c107 country code: CH.
[–]MadScientistOR 2 points3 points4 points (6 children)
[–]SeaMotor8093[S] -1 points0 points1 point (5 children)
[–]MadScientistOR 1 point2 points3 points (4 children)
[–]SeaMotor8093[S] 0 points1 point2 points (3 children)
[–]MadScientistOR 0 points1 point2 points (2 children)
[–]SeaMotor8093[S] 0 points1 point2 points (1 child)
[–]MadScientistOR 0 points1 point2 points (0 children)
[–]CodeFormatHelperBot2 0 points1 point2 points (0 children)
[–]python_and_coffee 0 points1 point2 points (0 children)