Related
Given the list of integers stored in the variable numbers, declare the variable result1 and initialize the values of the initial list containing only the even positive numbers, respecting the initial order.
numbers = [-85,34,-7,-6,57,-6,-65,-49,-51,79,90,-75,33,36,9,30,-61,-66,95 .15,-56.45,-21,-81,-83,-31.82,-92,21.95,-70.88,-25,100,-17,-52,-74,-24.85 ,-40,-35,82,-74,-89,73,-44,53,88,35,-77,-90, 54,-41,-23,58,-95,47,-65, -92,91,64,-69,-21,-58, -63,-51,95,45,95,50,4,-15,-98,27,-84,32,57,-50, -54,-14,-46,98,76,0,-85,-1,15,67,-5,92,-39,-68, -73,-85,0,-39,-72, -9,52,27,52,-41,69,52,-42, -78,-37.59,-77,-30,77,56,-13,-80,-14,-30.94 ,34, 81,-46,-77,50,-23,-20,-59,-4,59,94,34,50,46,87, -78,88,-47,66,92,- 42,0,-28,-31,-18,-32,34,66,-12, 81,17,-11,37,20,-82,9,41,28,-81,26,47, 70.98, -82,-15.46,-13.89,-33,19,94,-88,52,14,27,-4, 6,-60.44,-98.83,-13 ,-80,-58,-10,-33,-70.87, -50.51,-3,-36,-60.61,-40,41.91,-25.38,-60.67 ,-85.36,-83.43]
What I tried to do:
def filterList(p, xs):
return [x for x in xs if p(x)]
def ePar(numbers):
return numbers%2==0
def ePositive(numbers):
return numbers>0
def result1():
return filterList(eEven, ePositive)
print (result1[:7]) ## because i want the last seven even positive integers
I expected that with my function result1, it would filter just the even and positive integers, but instead, the error was the following:
***Run error***
Traceback (most recent call last):
File "__tester__.python3", line 35, in <module>
print(resultado1[:7])
TypeError: 'function' object is not subscriptable
The problem with your code is that your filterList function takes a function and a list of ints as an argument (type annotations added below for clarity):
from typing import Callable
def filterList(p: Callable[[int], bool], xs: list[int]) -> list[int]:
return [x for x in xs if p(x)]
but you're calling it with two functions:
return filterList(eEven, ePositive) # two Callable[[int], bool]s
You could write your filterList to take an arbitrary number of functions instead, like this:
def filterList(xs: list[int], *p: Callable[[int], bool]) -> list[int]:
return [x for x in xs if all(f(x) for f in p)]
and then call it like this:
def filterPositiveAndEven(xs: list[int]) -> list[int]:
return filterList(xs, ePar, ePositive)
numbers = [-85,34,-7,-6,57,-6,-65,-49,-51,79,90,-75,33,36,9,30,-61,-66,95.15,-56.45,-21,-81,-83,-31.82,-92,21.95,-70.88,-25,100,-17,-52,-74,-24.85 ,-40,-35,82,-74,-89,73,-44,53,88,35,-77,-90, 54,-41,-23,58,-95,47,-65, -92,91,64,-69,-21,-58, -63,-51,95,45,95,50,4,-15,-98,27,-84,32,57,-50, -54,-14,-46,98,76,0,-85,-1,15,67,-5,92,-39,-68, -73,-85,0,-39,-72, -9,52,27,52,-41,69,52,-42, -78,-37.59,-77,-30,77,56,-13,-80,-14,-30.94 ,34, 81,-46,-77,50,-23,-20,-59,-4,59,94,34,50,46,87, -78,88,-47,66,92,- 42,0,-28,-31,-18,-32,34,66,-12, 81,17,-11,37,20,-82,9,41,28,-81,26,47, 70.98, -82,-15.46,-13.89,-33,19,94,-88,52,14,27,-4, 6,-60.44,-98.83,-13 ,-80,-58,-10,-33,-70.87, -50.51,-3,-36,-60.61,-40,41.91,-25.38,-60.67 ,-85.36,-83.43]
print(filterPositiveAndEven(numbers))
# prints [34, 90, 36, 30, 100, 82, 88, 54, 58, 64, 50, 4, 32, 98, 76, 92, 52, 52, 52, 56, 34, 50, 94, 34, 50, 46, 88, 66, 92, 34, 66, 20, 28, 26, 94, 52, 14, 6]
You can conveniently use a generator for this as follows:
numbers = [-85,34,-7,-6,57,-6,-65,-49,-51,79,90,-75,33,36,9,30,-61,-66,95.15,-56.45,-21,-81,-83,-31.82,-92,21.95,-70.88,-25,100,-17,-52,-74,-24.85 ,-40,-35,82,-74,-89,73,-44,53,88,35,-77,-90, 54,-41,-23,58,-95,47,-65, -92,91,64,-69,-21,-58, -63,-51,95,45,95,50,4,-15,-98,27,-84,32,57,-50, -54,-14,-46,98,76,0,-85,-1,15,67,-5,92,-39,-68, -73,-85,0,-39,-72, -9,52,27,52,-41,69,52,-42, -78,-37.59,-77,-30,77,56,-13,-80,-14,-30.94 ,34, 81,-46,-77,50,-23,-20,-59,-4,59,94,34,50,46,87, -78,88,-47,66,92,- 42,0,-28,-31,-18,-32,34,66,-12, 81,17,-11,37,20,-82,9,41,28,-81,26,47, 70.98, -82,-15.46,-13.89,-33,19,94,-88,52,14,27,-4, 6,-60.44,-98.83,-13 ,-80,-58,-10,-33,-70.87, -50.51,-3,-36,-60.61,-40,41.91,-25.38,-60.67 ,-85.36,-83.43]
def even_pos(numlist):
for v in numlist:
if v > 0 and v % 2 == 0:
yield v
print([*even_pos(numbers)][-7:]) # last 7 values
Output:
[20, 28, 26, 94, 52, 14, 6]
I have no idea what your resultado1 or result1 variable is. I assume it to be the patent res variable used by programmers.
def filter_even_and_positive(num):
return (num % 2 == 0 and num > 0)
res = []
for n in numbers:
if filter_even_and_positive(n): res.append(n)
print(res[:7]) # First 7 values
With list comprehension you could shorten your code into:
res = [n for n in numbers if (n % 2 == 0 and n > 0)]
print(res[:7]) # First 7 Value
Output:
[34, 90, 36, 30, 100, 82, 88]
I am new to python and programming, so apologies in advance. I know of remove(), append(), len(), and rand.rang (or whatever it is), and I believe I would need those tools, but it's not clear to me how to code it.
What I would like to do is, while looping or otherwise accessing List_A, randomly select an index within List_A, remove the selected_index from List_A, and then append() the selected_index to List_B.
I would like to randomly remove only up to a certain percentage (or real number if this is impossible) of items from List A.
Any ideas?? Is what I'm describing possible?
If you don't care about the order of the input list, I'd shuffle it, then remove n items from that list, adding those to the other list:
from random import shuffle
def remove_percentage(list_a, percentage):
shuffle(list_a)
count = int(len(list_a) * percentage)
if not count: return [] # edge case, no elements removed
list_a[-count:], list_b = [], list_a[-count:]
return list_b
where percentage is a float value between 0.0 and 1.0.
Demo:
>>> list_a = range(100)
>>> list_b = remove_percentage(list_a, 0.25)
>>> len(list_a), len(list_b)
(75, 25)
>>> list_b
[1, 94, 13, 81, 23, 84, 41, 92, 74, 82, 42, 28, 75, 33, 35, 62, 2, 58, 90, 52, 96, 68, 72, 73, 47]
If you can find a random index i of some element in listA, then you can easily move it from A to B using:
listB.append(listA.pop(i))
>>> lis = range(100)
>>> per = .30
>>> no_of_items = int( len(lis) * per) #number of items in 30 percent
>>> lis_b = []
>>> for _ in xrange(no_of_items):
ind = random.randint(0,len(lis)-1) #selects a random index value
lis_b.append(lis.pop(ind)) #pop the item at that index and append to lis_b
...
>>> lis_b
[73, 32, 82, 68, 90, 19, 3, 49, 21, 17, 30, 75, 1, 31, 80, 48, 38, 18, 99, 98, 4, 20, 33, 29, 66, 41, 64, 26, 77, 95]
1) Calculate how many elements you want to remove, call it k.
2) random.randrange(len(listA)) will return a random number between 0 and len(listA)-1 inclusive, e.g. a random index you can use in listA.
3) Grab the element at that index, remove it from listA, append it to listB.
4) Repeat until you have removed k elements.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Fastest way to list all primes below N in python
I have not been doing programming for very long, and I'm just doing this for fun, and I don't know much advanced Python, but...
I wrote this, and I wanted to know whether it is actually an Eratosthenes Sieve program, and if it is, how could I make it faster. I don't really want someone to post a program that is a solution, but more tell me how I could adapt mine.
def eratSieve(n):
all = []
for a in range(2, n+1):
all.append(a)
for b in all:
for i in range(2,int(round(len(all)/b))):
while i*b in all:
all.remove(i*b)
i+=1
return all
Thanks for your help.
BTW - It's in Python 2.7
It does not work right.
The main problem is that you loop on all the value in all and in the while you remove some element from all.
This way some value in all are not considered, so the function does not remove all the non-prime numbers
Try to execute it for n=100 and the result you get is
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99
while it should be
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97
(from http://en.wikipedia.org/wiki/Prime_number)
Also, the range of the second for is wrong, since you consider the lenght of the list, not the current value of b and so you check for multiple of 2 only in the first 50 values, the multiple of 3 in the first 17, 5 in the first 9 and so on. From b = 13 you never enter in the inner for, since int(round(len(all)/b)) = 1 and so you have something like for i in range(2,1)
I agree with Gianluca, and I have a possible solution: keep your main array (all) as bools and mark non-primes, but don't remove them. It might also be faster, because you don't change list size.
Minor thing: you can just write all = range(2, n+1) in the beginning if you want to keep it as ints.
Your method produces incorrect results. The error lies in the for i loop. Here it is, adjusted, and with a test:
known_primes = [
2,3,5,7,11,13,17,19,23,29,
31,37,41,43,47,53,59,61,67,71,
73,79,83,89,97,101,103,107,109,113,
127,131,137,139,149,151,157,163,167,173,
179,181,191,193,197,199,211,223,227,229,
233,239,241,251,257,263,269,271,277,281,
283,293,307,311,313,317,331,337,347,349,
353,359,367,373,379,383,389,397,401,409,
419,421,431,433,439,443,449,457,461,463,
467,479,487,491,499,503,509,521,523,541,
547,557,563,569,571,577,587,593,599,601,
607,613,617,619,631,641,643,647,653,659,
661,673,677,683,691,701,709,719,727,733,
739,743,751,757,761,769,773,787,797,809,
811,821,823,827,829,839,853,857,859,863,
877,881,883,887,907,911,919,929,937,941,
947,953,967,971,977,983,991,997,1009,1013,
1019,1021,1031,1033,1039,1049,1051,1061,1063,1069,
1087,1091,1093,1097,1103,1109,1117,1123,1129,1151,
1153,1163,1171,1181,1187,1193,1201,1213,1217,1223,
1229,1231,1237,1249,1259,1277,1279,1283,1289,1291,
1297,1301,1303,1307,1319,1321,1327,1361,1367,1373,
1381,1399,1409,1423,1427,1429,1433,1439,1447,1451,
1453,1459,1471,1481,1483,1487,1489,1493,1499]
def eratSieve(n):
all = []
for a in range(2, n+1):
all.append(a)
for b in all:
for i in all[all.index(b):]:
while i*b in all:
all.remove(i*b)
i+=1
return all
for N in range(1500):
for n in eratSieve(N):
if n not in known_primes:
print N,n
def primes(N):
primes = [x for x in (2, 3, 5, 7, 11, 13) if x < N]
if N < 17: return primes
candidators = [x for x in xrange((N - 2) | 1, 15, -2)
if x % 3 and x % 5 and x % 7 and x % 11 and x % 13]
top = int(N ** 0.5)
while (top + 1) * (top + 1) <= N: top += 1
while True:
p = candidators.pop()
primes.append(p)
if p > top: break
candidators = filter(p.__rmod__, candidators)
candidators.reverse()
primes.extend(candidators)
return primes
I think this code would work faster...
This is my code for finding primes using the Sieve of Eratosthenes.
list = [i for i in range(2, int(raw_input("Compute primes up to what number? "))+1)]
for i in list:
for a in list:
if a!=i and a%i == 0:
list.remove(a)
Trying to find a way to compress those nested for loops into some kind of generator or comprehension, but it doesn't seem that you can apply a function to a list using a comprehension. I tried using map and filter, but I can't seem to get it right.
Thinking about something like this:
print map(list.remove(a), filter(lambda a, i: (a%i ==0 and a!=i), [(a, i) for i in list for a in list])
Obviously doesn't work for a dozen reasons. If I just was using the filter portion of that code:
filter(lambda a, i: (a%i ==0 and a!=i), **[(a, i) for i in list for a in list]**
What's the proper way of putting two variables into the lambda? (a,i) makes it a tuple, but I want to submit 'a' and 'i' as independent variables to put into the lambda.
I ended up resolving the problem with this one-liner:
print sorted(set([i for i in range(2, int(raw_input("Compute primes up to what number? "))+1)]).difference(a for i in l for a in l if a!=i and a%i == 0))
The first thing to note is that what you have written is not the sieve of eratosthenes. Look how many loops a totally naive sieve of eratosthenes executes:
def sieve1(n):
loops = 0
numbers = set(range(2, n))
for i in range(2, int(n ** 0.5) + 1):
for j in range(i * 2, n, i):
numbers.discard(j)
loops += 1
return sorted(numbers), loops
Tested:
>>> sieve1(100)
([2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,
43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97],
178)
178 loops for 100 numbers (not including the sort). This can be improved with a few minor changes:
def sieve2(n):
loops = 0
numbers = range(0, n)
for prime in numbers:
if prime < 2:
continue
elif prime > n ** 0.5:
break
for i in range(prime ** 2, n, prime):
numbers[i] = 0
loops += 1
return [x for x in numbers if x > 1], loops
Tested:
>>> sieve2(100)
([2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,
43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97],
102)
102 loops for 100 numbers (not including the filter at the end). Now look at yours:
def sieve3(n):
loops = 0
numbers = range(2, n)
for i in numbers:
for j in numbers:
if j != i and j % i == 0:
numbers.remove(j)
loops += 1
return numbers, loops
Tested:
>>> sieve3(100)
([2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,
43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97],
663)
It gets worse:
>>> [sieve1(x)[1] for x in [100, 1000, 10000]]
[178, 2978, 41723]
>>> [sieve2(x)[1] for x in [100, 1000, 10000]]
[102, 1409, 16979]
>>> [sieve3(x)[1] for x in [100, 1000, 10000]]
[663, 28986, 1523699]
At n = 10000, your implementation does almost 100x as much work!
My suggestion would be to create a sensible implementation before making it "compact." Code golf can be fun, but it's nowhere near as challenging or as edifying as writing efficient code, whatever the length.
That said, consider this one-liner (if you don't count the import, which you could get rid of by using lambda x, y: x - y in place of operator.sub). This implements the first algorithm with a small improvement:
>>> from operator import sub
>>> reduce(sub, (set(range(x ** 2, 100, x)) for x in range(2, int(100 ** 0.5) + 1)), set(range(2, 100)))
set([2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97])
It's not precisely a direct translation of your loops, but it's quite close and compact:
>>> l = range(2, 101)
>>> sorted(set(l).difference(a for i in l for a in l if a!=i and a%i == 0))
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
Although I'd suggest a > i rather than a != 0 as being shorter and faster ;)
You are not doing the Sieve of Eratosthenes; the danger of not properly implementing the algorithm is that it will be extremely slow. Try your algorithm on 10**6 for example.
Shortest implementation of the bounded Sieve of Eratosthenes I can come up with:
def primes(upTo):
isPrime = list(range(upTo))
for p in range(2,int(upTo**0.5)+1): #p: 2,3,4,...,sqrt(N)
print(p, isPrime[p])
if isPrime[p]:
for multiple in range(p**2,upTo,p): #mult: p^2, p^2+p, p^2+2p, ..., N
isPrime[multiple] = False
return [x for x in isPrime[2:] if x]
Demo:
>>> list(primes(29))
[2, 3, 5, 7, 11, 13, 17, 19, 23]
It's actually rather succinct, if you ignore linebreaks and the massive skip-even-numbers optimization:
isPrime=[True]*upTo for p in range(2,upTo): if isPrime[p]: yield p for m in range(p,upTo,p): isPrime[m]=False
Here is the most compact true sieve I have come up with so far. This performs surprisingly well.
def pgen(n): # Sieve of Eratosthenes generator
np = set() # keeps track of composite (not prime) numbers
for q in xrange(2, n+1):
if q not in np:
yield q
np.update(range(q*q, n+1, q))
>>> list(pgen(100))
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73,
79, 83, 89, 97]
This slightly more complex version is the fastest I have seen:
def pgen(n): # Sieve of Eratosthenes generator by Dan Salmonsen
yield 2
np = set()
for q in xrange(3, n+1, 2):
if q not in np:
yield q
np.update(range(q*q, n+1, q+q))
Here is a true sieve as a list comprehension:
def primes(n):
sieve = set(sum([range(q*q, n+1, q+q) for q in odds], []))
return [2] + [p for p in odds if p not in sieve]
>>> primes(100)
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73,
79, 83, 89, 97]
The following one-liner is not related at all to your code:
def primes(n):
return set(range(2,n))-{c for i in range(2,n) for c in range(2*i,n,i)}
Like your code, this is still not really the Sieve of Eratosthenes because, for example, it will futilely try to cross off multiples of 6 and 9 etc. Nevertheless it still runs significantly faster than most other Sieve look-alikes for values less than a million or more, since for small N there are "about as many" primes as non-primes (the fraction of numbers < N that are prime is 1/log(N)).
Heavily modified from source, possibly less efficient than original: http://codeblog.dhananjaynene.com/2011/06/10-python-one-liners-to-impress-your-friends/
Here's a simple demonstration of the sieve. Note that lambda isn't used as the filtering function, because the prime number needs to bound at definition time. Also of interest is that it's efficient in the sense of not duplicating divisions, but in the long run it could lead to you-know-what.
import itertools
def primes():
ints = itertools.count(2)
while True:
p = next(ints)
yield p
ints = itertools.ifilter(p.__rmod__, ints)
print list(itertools.islice(primes(), 10))
# [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
def sieve(n):
sieve_list = range(n)
zero_list = [0] * n
for i in range(2, int(n**.5) + 1):
if sieve_list[i]:
sieve_list[2*i:n:i] = zero_list[2*i:n:i]
return filter(None, sieve_list)[1:]
Not exactly the the most compact solution, but the step argument in the range function in Python3 helps here -
prime_sieve = [True] * (int(input('Primes Upto ?'))+1)
# The first prime number
for i in range(2, len(prime_sieve)):
if prime_sieve[i]:
for j in range(i+i, len(prime_sieve), i):
prime_sieve[j] = False
print(i, end=',')
In many languages we can do something like:
for (int i = 0; i < value; i++)
{
if (condition)
{
i += 10;
}
}
How can I do the same in Python? The following (of course) does not work:
for i in xrange(value):
if condition:
i += 10
I could do something like this:
i = 0
while i < value:
if condition:
i += 10
i += 1
but I'm wondering if there is a more elegant (pythonic?) way of doing this in Python.
Use continue.
for i in xrange(value):
if condition:
continue
If you want to force your iterable to skip forwards, you must call .next().
>>> iterable = iter(xrange(100))
>>> for i in iterable:
... if i % 10 == 0:
... [iterable.next() for x in range(10)]
...
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
[41, 42, 43, 44, 45, 46, 47, 48, 49, 50]
[61, 62, 63, 64, 65, 66, 67, 68, 69, 70]
[81, 82, 83, 84, 85, 86, 87, 88, 89, 90]
As you can see, this is disgusting.
Create the iterable before the loop.
Skip one by using next on the iterator
it = iter(xrange(value))
for i in it:
if condition:
i = next(it)
Skip many by using itertools or recipes based on ideas from itertools.
itertools.dropwhile()
it = iter(xrange(value))
for i in it:
if x<5:
i = dropwhile(lambda x: x<5, it)
Take a read through the itertools page, it shows some very common uses of working with iterators.
itertools islice
it = islice(xrange(value), 10)
for i in it:
...do stuff with i...
It's a very old question, but I find the accepted answer is not totally stisfactory:
first, after the if ... / [next()...] sequence, the value of i hasn't changed. In your first example, it has.
second, the list comprehension is used to produce a side-effect. This should be avoided.
third, there might be a faster way to achieve this.
Using a modified version of consume in itertools recipes, you can write:
import itertools
def consume(it, n):
return next(itertools.islice(it, n-1, n), None)
it = iter(range(20))
for i in it:
print(i, end='->')
if i%4 == 0:
i = consume(it, 5)
print(i)
As written in the doctstring of consume, the iterator is consumed at C speed (didn't benchmark though). Output:
0->5
6->6
7->7
8->13
14->14
15->15
16->None
With a minor modification, one can get 21 instead of None, but I think this isnot a good idea because this code does work with any iterable (otherwise one would prefer the while version):
import string
it = iter(string.ascii_lowercase) # a-z
for x in it:
print(x, end="->")
if x in set('aeiouy'):
x = consume(it, 2) # skip the two letters after the vowel
print(x)
Output:
a->c
d->d
e->g
h->h
i->k
l->l
m->m
n->n
o->q
r->r
s->s
t->t
u->w
x->x
y->None
Itertools has a recommended way to do this: https://docs.python.org/3.7/library/itertools.html#itertools-recipes
import collections
def tail(n, iterable):
"Return an iterator over the last n items"
# tail(3, 'ABCDEFG') --> E F G
return iter(collections.deque(iterable, maxlen=n))
Now you can do:
for i in tail(5, range(10)):
print(i)
to get
5
6
7
8
9
I am hoping I am not answering this wrong... but this is the simplest way I have come across:
for x in range(0,10,2):
print x
output should be something like this:
0
2
4
6
8
The 2 in the range parameter's is the jump value
Does a generator function here is rebundant?
Like this:
def filterRange(range, condition):
x = 0
while x < range:
x = (x+10) if condition(x) else (x + 1)
yield x
if __name__ == "__main__":
for i in filterRange(100, lambda x: x > 2):
print i
There are a few ways to create iterators, but the custom iterator class is the most extensible:
class skip_if: # skip_if(object) for python2
"""
iterates through iterable, calling skipper with each value
if skipper returns a positive integer, that many values are
skipped
"""
def __init__(self, iterable, skipper):
self.it = iter(iterable)
self.skip = skipper
def __iter__(self):
return self
def __next__(self): # def next(self): for python2
value = next(self.it)
for _ in range(self.skip(value)):
next(self.it, None)
return value
and in use:
>>> for i in skip_if(range(1,100), lambda n: 10 if not n%10 else 0):
... print(i, end=', ')
...
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
61, 62, 63, 64, 65, 66, 67, 68, 69, 70,
81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
I think you have to use a while loop for this...for loop loops over an iterable..and you cannot skip next item like how you want to do it here