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

  • SEARCH
  • Home
  • 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 9165187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:51:13+00:00 2026-06-17T14:51:13+00:00

I am working on Project Euler #35 , and I need to find the

  • 0

I am working on Project Euler #35, and I need to find the circular permutations of a number. Using itertools, I can easily get the permutations of a number. However, I want to do it with a list comprehension (as it seems more Pythonic; I am also trying to get familiar with list comprehensions).

I found that all circular primes can only contain the digits 1, 3, 7, and 9 (this excludes 2 and 5, which are circular primes by definition). If any other digit was in the number (0, 2, 4, 5, 6, or 8) one of the permutations would not be a prime (as that digit would be last in at least one of the permutations).

Thus, I tried doing this:

from itertools import permutations
l = [x for x in list(permutations('1397', y)) for y in range(7)]

I needed to use y for y in range(7) so that I get varying lengths of permutations.

However, this gave me a TypeError:

Traceback (most recent call last):
  File "<pyshell#23>", line 1, in <module>
    l = [x for x in list(permutations('1397', y)) for y in range(7)]
TypeError: an integer is required

This works, but it isn’t using two variables in one list comprehension:

l = []
for y in range(7):
    l.append([x for x in list(permutations('1379', y))])

How can I do a double-variable list comprehension? Thanks!

  • 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-17T14:51:14+00:00Added an answer on June 17, 2026 at 2:51 pm

    The for y in range(7) part should come before the permutation loop.:

    l = [x for y in range(7) for x in list(permutations('1397', y))]
    

    The above list comprehension is equivalent to :

    In [93]: l = []
    
    In [94]: for y in range(7):
        ...:     l.extend(list(permutations('1397', y)))
    

    For example:

    In [76]: l = [x for y in range(3) for x in list(permutations('1397', y))]
    
    In [77]: l
    Out[77]: 
    [(),
     ('1',),
     ('3',),
     ('9',),
     ('7',),
     ('1', '3'),
     ('1', '9'),
     ('1', '7'),
     ('3', '1'),
     ('3', '9'),
     ('3', '7'),
     ('9', '1'),
     ('9', '3'),
     ('9', '7'),
     ('7', '1'),
     ('7', '3'),
     ('7', '9')]
    

    And the list-comprehension version for your working example,

    l = []
    for y in range(7):
        l.append(list(permutations('1397', y)))
    

    is:

    In [85]: l = [list(permutations('1397', y)) for y in range(3)]
    
    In [86]: l
    Out[86]: 
    [[()],
     [('1',), ('3',), ('9',), ('7',)],
     [('1', '3'),
      ('1', '9'),
      ('1', '7'),
      ('3', '1'),
      ('3', '9'),
      ('3', '7'),
      ('9', '1'),
      ('9', '3'),
      ('9', '7'),
      ('7', '1'),
      ('7', '3'),
      ('7', '9')]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on Project Euler Problem 4 , and need to find the
When working with Project Euler problems I often need large (> 10**7) bit array's.
I'm currently working on project euler problem 14 . I solved it using a
I'm working on Project Euler problem number 205 which states: Peter has nine four-sided
I am working on Project Euler problem 5 and am using the following: def
I've started working on some Project Euler problems, and have solved number 4 with
Context: I'm working on Project Euler Problem 23 using Matlab in order to practice
I'm working on the project Euler problems, and #8 requires that you find the
I am currently working on Project Euler for fun, and use Haskell for practicing.
Working on the problems on Project Euler to try to learn Clojure. I'm on

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.