How to extract the elements of a deep nested tuple and put them in a list in python? [closed]












-1















I've been playing with Python, and wrote the following code that finds all prime numbers in a given range as following:



def get_primes(x):
primes =

def is_prime(x):
if x == 0:
return
else:
for i in range(2, int(x)):
if (x % i) == 0:
return is_prime(x - 1)
else:
return x, is_prime(x - 1)

primes.append(is_prime(x))
return primes


print(get_primes(int(input("Enter the range: 0 - "))))


And the output is: (enter 100 for example)



Enter the range: 0 - 100
[(97, (89, (83, (79, (73, (71, (67, (61, (59, (53, (47, (43, (41, (37, (31, (29, (23, (19, (17, (13, (11, (7, (5, (3, (2, (1, None))))))))))))))))))))))))))]


that looks so ugly.
So, I need a way to flatten the recursive tuple structure:



[97, 89, 83, 79, 73, 71, 67, 61, 59, 53, 47, 43, 41, 37, 31, 29, 23, 19, 17, 13, 11, 7, 5, 3, 2, 1]


i used the following code to do so:



x = get_primes(100)
arr =
arr.append(x[0][0])
arr.append(x[0][1][0])
arr.append(x[0][1][1][0])
arr.append(x[0][1][1][1][0])
arr.append(x[0][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
print(arr)


But of course, this is not a professional method.



So, what i want is to know how can i make this one:
[97, 89, 83, 79, 73, 71, 67, 61, 59, 53, 47, 43, 41, 37, 31, 29, 23, 19, 17, 13, 11, 7, 5, 3, 2, 1]



from this one:
[(97, (89, (83, (79, (73, (71, (67, (61, (59, (53, (47, (43, (41, (37, (31, (29, (23, (19, (17, (13, (11, (7, (5, (3, (2, (1, None))))))))))))))))))))))))))]




I found the answer here How to flatten a tuple in python but the code there was for python 2, so, i modified it a bit.




and used this code:



def flatten(T):
if type(T) is not tuple:
return (T,)
elif len(T) == 0:
return ()
else:
return flatten(T[0]) + flatten(T[1:])









share|improve this question















closed as unclear what you're asking by Jean-François Fabre, Goyo, pushkin, Prune, lagom Nov 20 '18 at 3:29


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.














  • 3





    Why does is_prime even return a tuple? it should return a boolean

    – DeepSpace
    Nov 19 '18 at 20:03











  • you could post process & flatten: stackoverflow.com/questions/2158395/… and fix your indentation

    – Jean-François Fabre
    Nov 19 '18 at 20:04








  • 2





    I don't understand your is_prime() function. When if (x % i) == 0 is true, you know x is not prime, so why do you continue?

    – John Gordon
    Nov 19 '18 at 20:05






  • 1





    Also - is there any particular reason to even attempt to use recursion here? It's not necessary and it won't scale.

    – Jon Clements
    Nov 19 '18 at 20:06











  • is_prime function will find all prime numbers in a specific range and return all these numbers, it checks if the current value of x is prime, if it is prime it returns this value and calls itself, otherwise it will call itself only without returning the value of x (which should be only prime number).

    – Dr.venom
    Nov 19 '18 at 21:01
















-1















I've been playing with Python, and wrote the following code that finds all prime numbers in a given range as following:



def get_primes(x):
primes =

def is_prime(x):
if x == 0:
return
else:
for i in range(2, int(x)):
if (x % i) == 0:
return is_prime(x - 1)
else:
return x, is_prime(x - 1)

primes.append(is_prime(x))
return primes


print(get_primes(int(input("Enter the range: 0 - "))))


And the output is: (enter 100 for example)



Enter the range: 0 - 100
[(97, (89, (83, (79, (73, (71, (67, (61, (59, (53, (47, (43, (41, (37, (31, (29, (23, (19, (17, (13, (11, (7, (5, (3, (2, (1, None))))))))))))))))))))))))))]


that looks so ugly.
So, I need a way to flatten the recursive tuple structure:



[97, 89, 83, 79, 73, 71, 67, 61, 59, 53, 47, 43, 41, 37, 31, 29, 23, 19, 17, 13, 11, 7, 5, 3, 2, 1]


i used the following code to do so:



x = get_primes(100)
arr =
arr.append(x[0][0])
arr.append(x[0][1][0])
arr.append(x[0][1][1][0])
arr.append(x[0][1][1][1][0])
arr.append(x[0][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
print(arr)


But of course, this is not a professional method.



So, what i want is to know how can i make this one:
[97, 89, 83, 79, 73, 71, 67, 61, 59, 53, 47, 43, 41, 37, 31, 29, 23, 19, 17, 13, 11, 7, 5, 3, 2, 1]



from this one:
[(97, (89, (83, (79, (73, (71, (67, (61, (59, (53, (47, (43, (41, (37, (31, (29, (23, (19, (17, (13, (11, (7, (5, (3, (2, (1, None))))))))))))))))))))))))))]




I found the answer here How to flatten a tuple in python but the code there was for python 2, so, i modified it a bit.




and used this code:



def flatten(T):
if type(T) is not tuple:
return (T,)
elif len(T) == 0:
return ()
else:
return flatten(T[0]) + flatten(T[1:])









share|improve this question















closed as unclear what you're asking by Jean-François Fabre, Goyo, pushkin, Prune, lagom Nov 20 '18 at 3:29


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.














  • 3





    Why does is_prime even return a tuple? it should return a boolean

    – DeepSpace
    Nov 19 '18 at 20:03











  • you could post process & flatten: stackoverflow.com/questions/2158395/… and fix your indentation

    – Jean-François Fabre
    Nov 19 '18 at 20:04








  • 2





    I don't understand your is_prime() function. When if (x % i) == 0 is true, you know x is not prime, so why do you continue?

    – John Gordon
    Nov 19 '18 at 20:05






  • 1





    Also - is there any particular reason to even attempt to use recursion here? It's not necessary and it won't scale.

    – Jon Clements
    Nov 19 '18 at 20:06











  • is_prime function will find all prime numbers in a specific range and return all these numbers, it checks if the current value of x is prime, if it is prime it returns this value and calls itself, otherwise it will call itself only without returning the value of x (which should be only prime number).

    – Dr.venom
    Nov 19 '18 at 21:01














-1












-1








-1








I've been playing with Python, and wrote the following code that finds all prime numbers in a given range as following:



def get_primes(x):
primes =

def is_prime(x):
if x == 0:
return
else:
for i in range(2, int(x)):
if (x % i) == 0:
return is_prime(x - 1)
else:
return x, is_prime(x - 1)

primes.append(is_prime(x))
return primes


print(get_primes(int(input("Enter the range: 0 - "))))


And the output is: (enter 100 for example)



Enter the range: 0 - 100
[(97, (89, (83, (79, (73, (71, (67, (61, (59, (53, (47, (43, (41, (37, (31, (29, (23, (19, (17, (13, (11, (7, (5, (3, (2, (1, None))))))))))))))))))))))))))]


that looks so ugly.
So, I need a way to flatten the recursive tuple structure:



[97, 89, 83, 79, 73, 71, 67, 61, 59, 53, 47, 43, 41, 37, 31, 29, 23, 19, 17, 13, 11, 7, 5, 3, 2, 1]


i used the following code to do so:



x = get_primes(100)
arr =
arr.append(x[0][0])
arr.append(x[0][1][0])
arr.append(x[0][1][1][0])
arr.append(x[0][1][1][1][0])
arr.append(x[0][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
print(arr)


But of course, this is not a professional method.



So, what i want is to know how can i make this one:
[97, 89, 83, 79, 73, 71, 67, 61, 59, 53, 47, 43, 41, 37, 31, 29, 23, 19, 17, 13, 11, 7, 5, 3, 2, 1]



from this one:
[(97, (89, (83, (79, (73, (71, (67, (61, (59, (53, (47, (43, (41, (37, (31, (29, (23, (19, (17, (13, (11, (7, (5, (3, (2, (1, None))))))))))))))))))))))))))]




I found the answer here How to flatten a tuple in python but the code there was for python 2, so, i modified it a bit.




and used this code:



def flatten(T):
if type(T) is not tuple:
return (T,)
elif len(T) == 0:
return ()
else:
return flatten(T[0]) + flatten(T[1:])









share|improve this question
















I've been playing with Python, and wrote the following code that finds all prime numbers in a given range as following:



def get_primes(x):
primes =

def is_prime(x):
if x == 0:
return
else:
for i in range(2, int(x)):
if (x % i) == 0:
return is_prime(x - 1)
else:
return x, is_prime(x - 1)

primes.append(is_prime(x))
return primes


print(get_primes(int(input("Enter the range: 0 - "))))


And the output is: (enter 100 for example)



Enter the range: 0 - 100
[(97, (89, (83, (79, (73, (71, (67, (61, (59, (53, (47, (43, (41, (37, (31, (29, (23, (19, (17, (13, (11, (7, (5, (3, (2, (1, None))))))))))))))))))))))))))]


that looks so ugly.
So, I need a way to flatten the recursive tuple structure:



[97, 89, 83, 79, 73, 71, 67, 61, 59, 53, 47, 43, 41, 37, 31, 29, 23, 19, 17, 13, 11, 7, 5, 3, 2, 1]


i used the following code to do so:



x = get_primes(100)
arr =
arr.append(x[0][0])
arr.append(x[0][1][0])
arr.append(x[0][1][1][0])
arr.append(x[0][1][1][1][0])
arr.append(x[0][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
arr.append(x[0][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][1][0])
print(arr)


But of course, this is not a professional method.



So, what i want is to know how can i make this one:
[97, 89, 83, 79, 73, 71, 67, 61, 59, 53, 47, 43, 41, 37, 31, 29, 23, 19, 17, 13, 11, 7, 5, 3, 2, 1]



from this one:
[(97, (89, (83, (79, (73, (71, (67, (61, (59, (53, (47, (43, (41, (37, (31, (29, (23, (19, (17, (13, (11, (7, (5, (3, (2, (1, None))))))))))))))))))))))))))]




I found the answer here How to flatten a tuple in python but the code there was for python 2, so, i modified it a bit.




and used this code:



def flatten(T):
if type(T) is not tuple:
return (T,)
elif len(T) == 0:
return ()
else:
return flatten(T[0]) + flatten(T[1:])






python tuples






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 13:27







Dr.venom

















asked Nov 19 '18 at 20:00









Dr.venomDr.venom

13




13




closed as unclear what you're asking by Jean-François Fabre, Goyo, pushkin, Prune, lagom Nov 20 '18 at 3:29


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.









closed as unclear what you're asking by Jean-François Fabre, Goyo, pushkin, Prune, lagom Nov 20 '18 at 3:29


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 3





    Why does is_prime even return a tuple? it should return a boolean

    – DeepSpace
    Nov 19 '18 at 20:03











  • you could post process & flatten: stackoverflow.com/questions/2158395/… and fix your indentation

    – Jean-François Fabre
    Nov 19 '18 at 20:04








  • 2





    I don't understand your is_prime() function. When if (x % i) == 0 is true, you know x is not prime, so why do you continue?

    – John Gordon
    Nov 19 '18 at 20:05






  • 1





    Also - is there any particular reason to even attempt to use recursion here? It's not necessary and it won't scale.

    – Jon Clements
    Nov 19 '18 at 20:06











  • is_prime function will find all prime numbers in a specific range and return all these numbers, it checks if the current value of x is prime, if it is prime it returns this value and calls itself, otherwise it will call itself only without returning the value of x (which should be only prime number).

    – Dr.venom
    Nov 19 '18 at 21:01














  • 3





    Why does is_prime even return a tuple? it should return a boolean

    – DeepSpace
    Nov 19 '18 at 20:03











  • you could post process & flatten: stackoverflow.com/questions/2158395/… and fix your indentation

    – Jean-François Fabre
    Nov 19 '18 at 20:04








  • 2





    I don't understand your is_prime() function. When if (x % i) == 0 is true, you know x is not prime, so why do you continue?

    – John Gordon
    Nov 19 '18 at 20:05






  • 1





    Also - is there any particular reason to even attempt to use recursion here? It's not necessary and it won't scale.

    – Jon Clements
    Nov 19 '18 at 20:06











  • is_prime function will find all prime numbers in a specific range and return all these numbers, it checks if the current value of x is prime, if it is prime it returns this value and calls itself, otherwise it will call itself only without returning the value of x (which should be only prime number).

    – Dr.venom
    Nov 19 '18 at 21:01








3




3





Why does is_prime even return a tuple? it should return a boolean

– DeepSpace
Nov 19 '18 at 20:03





Why does is_prime even return a tuple? it should return a boolean

– DeepSpace
Nov 19 '18 at 20:03













you could post process & flatten: stackoverflow.com/questions/2158395/… and fix your indentation

– Jean-François Fabre
Nov 19 '18 at 20:04







you could post process & flatten: stackoverflow.com/questions/2158395/… and fix your indentation

– Jean-François Fabre
Nov 19 '18 at 20:04






2




2





I don't understand your is_prime() function. When if (x % i) == 0 is true, you know x is not prime, so why do you continue?

– John Gordon
Nov 19 '18 at 20:05





I don't understand your is_prime() function. When if (x % i) == 0 is true, you know x is not prime, so why do you continue?

– John Gordon
Nov 19 '18 at 20:05




1




1





Also - is there any particular reason to even attempt to use recursion here? It's not necessary and it won't scale.

– Jon Clements
Nov 19 '18 at 20:06





Also - is there any particular reason to even attempt to use recursion here? It's not necessary and it won't scale.

– Jon Clements
Nov 19 '18 at 20:06













is_prime function will find all prime numbers in a specific range and return all these numbers, it checks if the current value of x is prime, if it is prime it returns this value and calls itself, otherwise it will call itself only without returning the value of x (which should be only prime number).

– Dr.venom
Nov 19 '18 at 21:01





is_prime function will find all prime numbers in a specific range and return all these numbers, it checks if the current value of x is prime, if it is prime it returns this value and calls itself, otherwise it will call itself only without returning the value of x (which should be only prime number).

– Dr.venom
Nov 19 '18 at 21:01












2 Answers
2






active

oldest

votes


















4














You can adapt your function to be a generator pretty easily:



def is_prime(x):
if x != 0:
for i in range(2, int(x)):
if (x % i) == 0:
yield from is_prime(x - 1)
return
else:
yield x
yield from is_prime(x - 1)


Then to get all of the primes up to 100 you can pass that generator to list to get a list containing the values from that generator:



print(list(is_prime(100)))
# [97, 89, 83, 79, 73, 71, 67, 61, 59, 53, 47, 43, 41, 37, 31, 29, 23, 19, 17, 13, 11, 7, 5, 3, 2, 1]


Recursion isn't a great approach to this though, I would suggest looking up the Sieve of Eratosthenes for a better approach.



The yield from syntax is only available in newer versions of Python (>= 3.3). You can replace



yield from x


with



for y in x:
yield y


if necessary.






share|improve this answer
























  • Thanks for clarification, this is definitely a better way to generate these prime numbers in a specific range, but my question is about how can i get these values out from the returned list: [(97, (89, (83, (79, (73, (71, (67, (61, (59, (53, (47, (43, (41, (37, (31, (29, (23, (19, (17, (13, (11, (7, (5, (3, (2, (1, None))))))))))))))))))))))))))]

    – Dr.venom
    Nov 19 '18 at 21:19





















1














If you create a nested tuple, then the only way to flatten it is by unpacking. Best way to not deal with that is just not creating one.
Here goes an alternative version of your code:



def get_primes(limit):

def is_prime(x):
if x in (2, 3):
return True
if (x % 2 == 0) or (x % 3 == 0):
return False
i, w = 5, 2
while i**2 <= x:
if x % i == 0:
return False
i += w
w = 6 - w
return True

return [x for x in range(2, limit + 1) if is_prime(x)]

print(get_primes(int(input("Enter the range: 0 - "))))





share|improve this answer






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4














    You can adapt your function to be a generator pretty easily:



    def is_prime(x):
    if x != 0:
    for i in range(2, int(x)):
    if (x % i) == 0:
    yield from is_prime(x - 1)
    return
    else:
    yield x
    yield from is_prime(x - 1)


    Then to get all of the primes up to 100 you can pass that generator to list to get a list containing the values from that generator:



    print(list(is_prime(100)))
    # [97, 89, 83, 79, 73, 71, 67, 61, 59, 53, 47, 43, 41, 37, 31, 29, 23, 19, 17, 13, 11, 7, 5, 3, 2, 1]


    Recursion isn't a great approach to this though, I would suggest looking up the Sieve of Eratosthenes for a better approach.



    The yield from syntax is only available in newer versions of Python (>= 3.3). You can replace



    yield from x


    with



    for y in x:
    yield y


    if necessary.






    share|improve this answer
























    • Thanks for clarification, this is definitely a better way to generate these prime numbers in a specific range, but my question is about how can i get these values out from the returned list: [(97, (89, (83, (79, (73, (71, (67, (61, (59, (53, (47, (43, (41, (37, (31, (29, (23, (19, (17, (13, (11, (7, (5, (3, (2, (1, None))))))))))))))))))))))))))]

      – Dr.venom
      Nov 19 '18 at 21:19


















    4














    You can adapt your function to be a generator pretty easily:



    def is_prime(x):
    if x != 0:
    for i in range(2, int(x)):
    if (x % i) == 0:
    yield from is_prime(x - 1)
    return
    else:
    yield x
    yield from is_prime(x - 1)


    Then to get all of the primes up to 100 you can pass that generator to list to get a list containing the values from that generator:



    print(list(is_prime(100)))
    # [97, 89, 83, 79, 73, 71, 67, 61, 59, 53, 47, 43, 41, 37, 31, 29, 23, 19, 17, 13, 11, 7, 5, 3, 2, 1]


    Recursion isn't a great approach to this though, I would suggest looking up the Sieve of Eratosthenes for a better approach.



    The yield from syntax is only available in newer versions of Python (>= 3.3). You can replace



    yield from x


    with



    for y in x:
    yield y


    if necessary.






    share|improve this answer
























    • Thanks for clarification, this is definitely a better way to generate these prime numbers in a specific range, but my question is about how can i get these values out from the returned list: [(97, (89, (83, (79, (73, (71, (67, (61, (59, (53, (47, (43, (41, (37, (31, (29, (23, (19, (17, (13, (11, (7, (5, (3, (2, (1, None))))))))))))))))))))))))))]

      – Dr.venom
      Nov 19 '18 at 21:19
















    4












    4








    4







    You can adapt your function to be a generator pretty easily:



    def is_prime(x):
    if x != 0:
    for i in range(2, int(x)):
    if (x % i) == 0:
    yield from is_prime(x - 1)
    return
    else:
    yield x
    yield from is_prime(x - 1)


    Then to get all of the primes up to 100 you can pass that generator to list to get a list containing the values from that generator:



    print(list(is_prime(100)))
    # [97, 89, 83, 79, 73, 71, 67, 61, 59, 53, 47, 43, 41, 37, 31, 29, 23, 19, 17, 13, 11, 7, 5, 3, 2, 1]


    Recursion isn't a great approach to this though, I would suggest looking up the Sieve of Eratosthenes for a better approach.



    The yield from syntax is only available in newer versions of Python (>= 3.3). You can replace



    yield from x


    with



    for y in x:
    yield y


    if necessary.






    share|improve this answer













    You can adapt your function to be a generator pretty easily:



    def is_prime(x):
    if x != 0:
    for i in range(2, int(x)):
    if (x % i) == 0:
    yield from is_prime(x - 1)
    return
    else:
    yield x
    yield from is_prime(x - 1)


    Then to get all of the primes up to 100 you can pass that generator to list to get a list containing the values from that generator:



    print(list(is_prime(100)))
    # [97, 89, 83, 79, 73, 71, 67, 61, 59, 53, 47, 43, 41, 37, 31, 29, 23, 19, 17, 13, 11, 7, 5, 3, 2, 1]


    Recursion isn't a great approach to this though, I would suggest looking up the Sieve of Eratosthenes for a better approach.



    The yield from syntax is only available in newer versions of Python (>= 3.3). You can replace



    yield from x


    with



    for y in x:
    yield y


    if necessary.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 19 '18 at 20:08









    Patrick HaughPatrick Haugh

    28.7k82747




    28.7k82747













    • Thanks for clarification, this is definitely a better way to generate these prime numbers in a specific range, but my question is about how can i get these values out from the returned list: [(97, (89, (83, (79, (73, (71, (67, (61, (59, (53, (47, (43, (41, (37, (31, (29, (23, (19, (17, (13, (11, (7, (5, (3, (2, (1, None))))))))))))))))))))))))))]

      – Dr.venom
      Nov 19 '18 at 21:19





















    • Thanks for clarification, this is definitely a better way to generate these prime numbers in a specific range, but my question is about how can i get these values out from the returned list: [(97, (89, (83, (79, (73, (71, (67, (61, (59, (53, (47, (43, (41, (37, (31, (29, (23, (19, (17, (13, (11, (7, (5, (3, (2, (1, None))))))))))))))))))))))))))]

      – Dr.venom
      Nov 19 '18 at 21:19



















    Thanks for clarification, this is definitely a better way to generate these prime numbers in a specific range, but my question is about how can i get these values out from the returned list: [(97, (89, (83, (79, (73, (71, (67, (61, (59, (53, (47, (43, (41, (37, (31, (29, (23, (19, (17, (13, (11, (7, (5, (3, (2, (1, None))))))))))))))))))))))))))]

    – Dr.venom
    Nov 19 '18 at 21:19







    Thanks for clarification, this is definitely a better way to generate these prime numbers in a specific range, but my question is about how can i get these values out from the returned list: [(97, (89, (83, (79, (73, (71, (67, (61, (59, (53, (47, (43, (41, (37, (31, (29, (23, (19, (17, (13, (11, (7, (5, (3, (2, (1, None))))))))))))))))))))))))))]

    – Dr.venom
    Nov 19 '18 at 21:19















    1














    If you create a nested tuple, then the only way to flatten it is by unpacking. Best way to not deal with that is just not creating one.
    Here goes an alternative version of your code:



    def get_primes(limit):

    def is_prime(x):
    if x in (2, 3):
    return True
    if (x % 2 == 0) or (x % 3 == 0):
    return False
    i, w = 5, 2
    while i**2 <= x:
    if x % i == 0:
    return False
    i += w
    w = 6 - w
    return True

    return [x for x in range(2, limit + 1) if is_prime(x)]

    print(get_primes(int(input("Enter the range: 0 - "))))





    share|improve this answer




























      1














      If you create a nested tuple, then the only way to flatten it is by unpacking. Best way to not deal with that is just not creating one.
      Here goes an alternative version of your code:



      def get_primes(limit):

      def is_prime(x):
      if x in (2, 3):
      return True
      if (x % 2 == 0) or (x % 3 == 0):
      return False
      i, w = 5, 2
      while i**2 <= x:
      if x % i == 0:
      return False
      i += w
      w = 6 - w
      return True

      return [x for x in range(2, limit + 1) if is_prime(x)]

      print(get_primes(int(input("Enter the range: 0 - "))))





      share|improve this answer


























        1












        1








        1







        If you create a nested tuple, then the only way to flatten it is by unpacking. Best way to not deal with that is just not creating one.
        Here goes an alternative version of your code:



        def get_primes(limit):

        def is_prime(x):
        if x in (2, 3):
        return True
        if (x % 2 == 0) or (x % 3 == 0):
        return False
        i, w = 5, 2
        while i**2 <= x:
        if x % i == 0:
        return False
        i += w
        w = 6 - w
        return True

        return [x for x in range(2, limit + 1) if is_prime(x)]

        print(get_primes(int(input("Enter the range: 0 - "))))





        share|improve this answer













        If you create a nested tuple, then the only way to flatten it is by unpacking. Best way to not deal with that is just not creating one.
        Here goes an alternative version of your code:



        def get_primes(limit):

        def is_prime(x):
        if x in (2, 3):
        return True
        if (x % 2 == 0) or (x % 3 == 0):
        return False
        i, w = 5, 2
        while i**2 <= x:
        if x % i == 0:
        return False
        i += w
        w = 6 - w
        return True

        return [x for x in range(2, limit + 1) if is_prime(x)]

        print(get_primes(int(input("Enter the range: 0 - "))))






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 '18 at 20:47









        AResemAResem

        1114




        1114















            Popular posts from this blog

            鏡平學校

            ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

            Guess what letter conforming each word