Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9119235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:20:52+00:00 2026-06-17T05:20:52+00:00

I have the following list in Python: l = [[2], [3], [2, 2], [5],

  • 0

I have the following list in Python:

l = [[2], [3], [2, 2], [5], [2], [3], [7], [2, 2, 2], [3, 3], [2], [5], [11], [2, 2], [3], [13], [2], [7], [3], [5], [2, 2, 2, 2], [17], [2], [3, 3], [19], [2, 2], [5]]

I want to write a function that will return the uniquely-valued sublists of maximum length. In this case, the function would return:

l = [[5], [7], [3, 3], [11], [13], [2, 2, 2, 2], [17], [19]]

I am still a beginner at python, and I have very little idea as to how to write such a function, however. The furthest I got was figuring out that I could iterate over the sublists by using nested loops. But from what I’ve seen of Python, it seems like there must be some simpler way to return the list I am looking for than using loops.

Update:

Here’s what I was doing with the code: solving project euler #5, the non-brute force way!

I’m sure this code could be refactored, but whatever.

Thanks for your help, guys. itemgetter was just what I needed.

#!/usr/bin/python
# coding = UTF-8

import argparse, sys, math
from itertools import groupby
from collections import defaultdict
from operator import itemgetter

parser = argparse.ArgumentParser()
parser.add_argument('filename', nargs='?')
args = parser.parse_args()
if args:
   intinput = int(sys.argv[1])
elif not sys.stdin.isatty():
    intinput = int(sys.stdin.read())
else:
    parser.print_help()

def prime_factorize(n):
    factors = []
    number = math.fabs(n)

    while number > 1:
        factor = get_next_prime_factor(number)
        factors.append(factor)
        number /= factor

    if n < -1: 
        factors[0] = -factors[0]

    return factors

def get_next_prime_factor(n):
    if n % 2 == 0:
        return 2

    for x in range(3, int(math.ceil(math.sqrt(n)) + 1), 2):
        if n % x == 0:
            return x

    return int(n)


def mkfactors(n):
  tpf = []
  for i in range(n+1):
    tpf.extend(prime_factorize(i))
  return tpf

l = [list(g) for k,g in groupby(mkfactors(intinput))]

m = [max(g) for _,g in groupby(sorted(l,key=itemgetter(0)),key=itemgetter(0))]


prod = 1

for list in m:
  for element in list:
    prod *= element

print prod
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:20:54+00:00Added an answer on June 17, 2026 at 5:20 am
    from itertools import groupby
    from operator import itemgetter
    
    [max(g) for _,g in groupby(sorted(l),key=itemgetter(0))]
    

    out:

    [[2, 2, 2, 2], [3, 3], [5], [7], [11], [13], [17], [19]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets suppose that I have a list of floats and I want to write
I have the following list in Python: [[1, 2], [3, 4], [4, 6], [2,
I have list in python which has following entries name-1 name-2 name-3 name-4 name-1
I have the following list of time zones that a user can choose from,
I'm attempting to write a Python function to send an email to a list
I want to send a python list to my ladon service. Consider the following
I have the following Python list (can also be a tuple): myList = ['foo',
I have a Python list of data: [1,2,3,4,5] I want to read this data
I have the following code to return multiple values from pl/python: CREATE TYPE named_value
Assuming I have a list of objects that have the following fields parent value

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.