Skip to content
Advertisement

How can I replace items within a list in python3 using a condition?

Write a function which prints all integers between 0 and 100: For every number that is divisible by 7 or has the digit 7 print ‘boom’ instead of the number itself.

My solution so far:

def boom():
r = []
for i in range(1, 100):
    if i % 7 == 0 or '7' in str(i):
        r.append("boom")
    else:
        r.append(i)
return r

print(boom())

Helped by: AkshayNevrekar

Question: Do you find a better way to solve this specific problem? a better algorithm? Thanks!

Advertisement

Answer

You can try this.

You need to check if i is divisible by 7 if yes then add 'BOOM' to your result list. Now if a number has digit 7 in it or not has to be checked you can typecast i to string and use in to check if 7 is present or not. If i not divisible by 7 and it doesn’t contain 7 in it then just append i to your result list.

def boom():
    return ['BOOM' if i%7==0 or '7' in str(i) else i for i in range(1,101)]
print(boom())

[1, 2, 3, 4, 5, 6, 'BOOM', 8, 9, 10, 11, 12, 13, 'BOOM', 15, 16, 'BOOM', 18, 19, 20, 'BOOM', 22, 23, 24, 25, 26, 'BOOM', 'BOOM', 29, 30, 31, 32, 33, 34, 'BOOM', 36, 'BOOM', 38, 39, 40, 41, 'BOOM', 43, 44, 45, 46, 'BOOM', 48, 'BOOM', 50, 51, 52, 53, 54, 55, 'BOOM', 'BOOM', 58, 59, 60, 61, 62, 'BOOM', 64, 65, 66, 'BOOM', 68, 69, 'BOOM', 'BOOM', 'BOOM', 'BOOM', 'BOOM', 'BOOM', 'BOOM', 'BOOM', 'BOOM', 'BOOM', 80, 81, 82, 83, 'BOOM', 85, 86, 'BOOM', 88, 89, 90, 'BOOM', 92, 93, 94, 95, 96, 'BOOM', 'BOOM', 99, 100]

def boom(n:int,inp:str):
    return [inp if i%7==0 or '7' in str(i) else i for i in range(1,n+1)]

print(boom(20,'7 used to be here'))

for i in boom(20,'7 used to be here'):
    print(i)

Here, n is range and inp is string that replaces the number which has 7 in it or divisible by 7.


output

[1, 2, 3, 4, 5, 6, '7 used to be here', 8, 9, 10, 11, 12, 13, '7 used to be here', 15, 16, '7 used to be here', 18, 19, 20]

1
2
3
4
5
6
7 used to be here
8
9
10
11
12
13
7 used to be here
15
16
7 used to be here
18
19
20

Try this to make your boom() complete.

def boom(n=100,inp='BOOM'):
    return [inp if i%7==0 or '7' in str(i) else i for i in range(1,n+1)]
  1. If boom() is called with no parameters then it considers n to be 101 and inp as 'BOOM'.
  2. If boom(n=x) is called then it considers range to be x+1 and inp as 'BOOM'
  3. If boom(inp=any_string) is called then it considers range to be 101 and inp as any_string.
  4. if boom(n=x,inp=any_string) is called then it considers range to be x+1 and inp to be any_string.

output

>>> boom()
[1, 2, 3, 4, 5, 6, 'BOOM', 8, 9, 10, 11, 12, 13, 'BOOM', 15, 16, 'BOOM', 18, 19, 20, 'BOOM', 22, 23, 24, 25, 26, 'BOOM', 'BOOM', 29, 30, 31, 32, 33, 34, 'BOOM', 36, 'BOOM', 38, 39, 40, 41, 'BOOM', 43, 44, 45, 46, 'BOOM', 48, 'BOOM', 50, 51, 52, 53, 54, 55, 'BOOM', 'BOOM', 58, 59, 60, 61, 62, 'BOOM', 64, 65, 66, 'BOOM', 68, 69, 'BOOM', 'BOOM', 'BOOM', 'BOOM', 'BOOM', 'BOOM', 'BOOM', 'BOOM', 'BOOM', 'BOOM', 80, 81, 82, 83, 'BOOM', 85, 86, 'BOOM', 88, 89, 90, 'BOOM', 92, 93, 94, 95, 96, 'BOOM', 'BOOM', 99, 100]
>>> boom(50)
[1, 2, 3, 4, 5, 6, 'BOOM', 8, 9, 10, 11, 12, 13, 'BOOM', 15, 16, 'BOOM', 18, 19, 20, 'BOOM', 22, 23, 24, 25, 26, 'BOOM', 'BOOM', 29, 30, 31, 32, 33, 34, 'BOOM', 36, 'BOOM', 38, 39, 40, 41, 'BOOM', 43, 44, 45, 46, 'BOOM', 48, 'BOOM', 50]
>>> boom(inp='hehe')
[1, 2, 3, 4, 5, 6, 'hehe', 8, 9, 10, 11, 12, 13, 'hehe', 15, 16, 'hehe', 18, 19, 20, 'hehe', 22, 23, 24, 25, 26, 'hehe', 'hehe', 29, 30, 31, 32, 33, 34, 'hehe', 36, 'hehe', 38, 39, 40, 41, 'hehe', 43, 44, 45, 46, 'hehe', 48, 'hehe', 50, 51, 52, 53, 54, 55, 'hehe', 'hehe', 58, 59, 60, 61, 62, 'hehe', 64, 65, 66, 'hehe', 68, 69, 'hehe', 'hehe', 'hehe', 'hehe', 'hehe', 'hehe', 'hehe', 'hehe', 'hehe', 'hehe', 80, 81, 82, 83, 'hehe', 85, 86, 'hehe', 88, 89, 90, 'hehe', 92, 93, 94, 95, 96, 'hehe', 'hehe', 99, 100]
>>> boom(n=77,inp='77777')
[1, 2, 3, 4, 5, 6, '77777', 8, 9, 10, 11, 12, 13, '77777', 15, 16, '77777', 18, 19, 20, '77777', 22, 23, 24, 25, 26, '77777', '77777', 29, 30, 31, 32, 33, 34, '77777', 36, '77777', 38, 39, 40, 41, '77777', 43, 44, 45, 46, '77777', 48, '77777', 50, 51, 52, 53, 54, 55, '77777', '77777', 58, 59, 60, 61, 62, '77777', 64, 65, 66, '77777', 68, 69, '77777', '77777', '77777', '77777', '77777', '77777', '77777', '77777']
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement