r/CodingHelp • u/motelvirginmary • Oct 04 '21
[Python] how can I write my code more efficiently?
6
u/ashirviskas Oct 04 '21
I'd work on readability and the lack of whitespace. Also, you're running the loops redundantly. Here's how I'd write it:
multiples_a = []
multiples_b = []
multiples_c = []
for i in range(5, 26):
if i % 3 == 0 or i % 5 == 0:
multiples_a.append(i)
if i % 3 == 0 and i % 5 != 0:
multiples_b.append(i)
if i % 3 != 0 and i % 5 == 0:
multiples_c.append(i)
print(*multiples_a)
print(*multiples_b)
print(*multiples_c)Wish you luck on your programming journey!
1
2
u/yes_i_relapsed Oct 04 '21
In case you want the code to consume less resources (not usually a concern, since we've already decided to use python) you could compose your list like this:
multiples = [ str(x) for x in range(5,26) if <whatever> ]It saves you having to generate a second list and makes the following line just
string = ' '.join(multiples)If you were doing math with the numbers, it would make more sense to keep them as ints and do what you're doing. But you're just printing them out.
1
1
u/Goobyalus Oct 04 '21 edited Oct 04 '21
Here is an "efficient" way:
>>> print(*(x for x in range(5, 26) if x % 3 == 0 or x % 5 == 0))
5 6 9 10 12 15 18 20 21 24 25
>>> print(*(x for x in range(5, 26) if x % 3 == 0 and x % 5 != 0))
6 9 12 18 21 24
>>> print(*(x for x in range(5, 26) if x % 3 != 0 and x % 5 != 0))
7 8 11 13 14 16 17 19 22 23You're already using what's called "generator expressions." Generator expressions evaluate to a generator object, and generator objects allow you to iterate through values without storing the whole sequence in memory.
For example, when you do str(e) for e in multiples, that expression evaluates to a generator object, which you pass to join.
We can create the desired generators like this:
>>> (x for x in range(5, 26) if x % 3 != 0 and x % 5 != 0)
<generator object <genexpr> at 0x00000261FAB8DAC0>If we try to print this, we'll get the string representation of the generator object (like you see above).
print accepts an arbitrary number of arguments, and by default separates them with spaces (similar to join). We can pass each element of our generator to print by unpacking it with the * operator:
>>> print(*(x for x in range(5, 26) if x % 3 == 0 or x % 5 == 0))
5 6 9 10 12 15 18 20 21 24 25If your generators create very large sequences, this still puts the entire sequence into memory at the point where we call print, so it would not be memory efficient. You would want to print element by element or in chunks. You can adjust the end of a print statement to be something besides a newline.
Here's an example of unpacking.
A function that prints out an enumeration of its arguments:
>>> def f(*args):
... for i, arg in enumerate(args):
... print(f"argument {i}:\t{arg}")
...A simple list:
>>> b = [1, 2, 3]Passing the list to f:
>>> f(b)
argument 0: [1, 2, 3]Passing the unpacked list to f:
>>> f(*b)
argument 0: 1
argument 1: 2
argument 2: 30
-1
-3
21
u/icjeremy Oct 04 '21 edited Oct 04 '21
What do you mean by efficiently, exactly?
Like not repeat code so much? Especially since, if you wanted to change something like the range, you'd have to change it in 3 places. In this case, factor commonalities into a function. Something like: