-2

Okay, first of all, I will put a code that doesn't relate to what I want, but its basically close to what I want.

def perfect(n): 
    return n == sum(i for i in range(1, n) if not n % i) 

this function is basically perfect number but in one line.

I have a question to make list of all the commons of a number ( if number is 8, then list = [1, 2, 4, 8]. I wanted to try something like that, in one or two line, but I could not really manage to do it... I know how to solve it with a few lines, but I want to try to be better and more efficient in coding.

I tried this:

def common_number(number):
    empty_list = []
    return empty_list.append() = (common for common in range(1, number + 1)) if number % common == 0

is there any way of making the code efficient like the perfect number, but in the common, in a way it will work? ill be happy to know if there is a way. You can just give me a tip ( its preferrable, not an answer ). Thanks in regards.

EDIT: I know how to solve the code, its an easy question:

def common_number(number):
    empty_list = []
    for common in range(1, number + 1):
        if number % common == 0:
            empty_list.append(common)
    return empty_list

x = common_number(8)
print(x)

But as said, I want by efficient like I did above ( someone helped me like that )

  • ```return [common for common in range(1,number+1) if number % common==0]```, if i got you correct? –  Aug 22 '21 at 17:44
  • 1
    Welcome to Stack Overflow. Please try putting `python list comprehension` into a search engine and see if you can work it out. You really already know what you need to except for a bit of fundamental syntax, and Stack Overflow is *not the place to learn that*; you should follow a tutorial in order to learn language fundamentals. The official python.org website provides one. – Karl Knechtel Aug 22 '21 at 17:44
  • You could also try reading https://treyhunner.com/2015/12/python-list-comprehensions-now-in-color/ . – Karl Knechtel Aug 22 '21 at 17:44
  • Welcome to Stack Overflow! Please take the [tour] and read [ask]. Efficiency means *how your code performs*, but this code doesn't perform at all - it's syntactically invalid. If I were you, I would change the question to be, "How can I build a list in one line?" Secondly, when you say "common", you mean "factor", right? You might have gotten confused by the term "common factor". – wjandrea Aug 22 '21 at 17:53
  • @Sujay Dam, its awesome!! how did you think of it!!! Thanks alot!!!......................................... to Karl Thanks (about the welcome ), about python list comprehension, ill check it out, as well as the link you gave me..................... to wjandrea yea, it is weird to me too, the link is not duplicated... but atleast i got my solution, so its all good, thanks :) – Ben Shaines Aug 22 '21 at 17:55
  • @Sujay but just one question, how did it become a list? because of the return [] thingy? because its a list? – Ben Shaines Aug 22 '21 at 17:56
  • ```[]``` it is due to the square brackets that define a list –  Aug 22 '21 at 17:56
  • @Sujay Thanks! I started reading the link Karl gave me, its pretty hard but I guess ill try to learn it, I understand its really important to code like that. about wjandrea - just saw your second comment, the fact you said it doesnt perform, about the first code yea, the second does perform ( you probably didnt see the edit ). but nvm, I got my answer so its all good :) – Ben Shaines Aug 22 '21 at 18:00
  • 1
    Edited in. I'm surprised you don't have a gold [python] badge yet. – Karl Knechtel Aug 22 '21 at 18:05
  • @Ben I'm talking about the second snippet -- the one that's syntactically invalid. You seem to be talking about the third one. BTW, if you don't @-tag me, I don't get a notification. I just happened to still be on this page. But you can only @-tag one person per comment, so you'd need to break up your multi-part comments. – wjandrea Aug 22 '21 at 18:09
  • @Karl Thanks! I got to the party late (2016), so most of the good questions have already been asked and answered :p – wjandrea Aug 22 '21 at 18:12
  • @wjandrea oh yea, I talked about the third one, my bad.////// yea, I know you talked about the second, It actually works, I dont see why it wont work? check it on pycharm. Anyway it doesnt really matter as I got answers :)////// and sorry about the tag hehe, Im pretty new to stack. – Ben Shaines Aug 22 '21 at 18:19

1 Answers1

1

You want a list comprehension:

def common_number(number):
    return [c for c in range(1, number+1) if number % c == 0]
Samwise
  • 51,883
  • 3
  • 26
  • 36
  • Oh just saw this answer, I thought it got closed so I didnt bother to check. yea, exactly, like the code you did. Thanks too :) – Ben Shaines Aug 22 '21 at 18:01