Python code best-practice questions

Discussion in 'Programming & Software Development' started by neRok, May 28, 2013.

  1. neRok

    neRok Member

    Joined:
    Aug 19, 2006
    Messages:
    4,166
    Location:
    Perth NOR
    I am just a 'hobby' programmer, and ive mucked around with php and drupal for a while, so have some 'habits' from there. I am doing some python tutorials now, and am wondering the best way to approach some things.

    In the tutorial now, I am adding a string to the end of a list. It is suggesting;
    Code:
    list.append(string)
    However, I think I would prefer to write;
    Code:
    list += [string]
    that is much more like I already know. Anything wrong with that?
     
    Last edited: May 28, 2013
  2. Rezin

    Rezin Member

    Joined:
    Oct 27, 2002
    Messages:
    9,485
    I don't know Python so, stupid question... does the += actually work?
     
  3. OP
    OP
    neRok

    neRok Member

    Joined:
    Aug 19, 2006
    Messages:
    4,166
    Location:
    Perth NOR
    Yes. Im more thinking if no one else ever codes that way with python and might read my code and think wtf, i might just go with the append method (even though it is more typing). Maybe one has a performance advantage over the other?
     
  4. joe_sixpack

    joe_sixpack Member

    Joined:
    Jan 21, 2002
    Messages:
    2,850
    Location:
    Brisbane
    You can't use '+=' to add a single new string to your list..

    It will basically do this:

    list = ['hello']

    list += 'bye'

    list would now look like this:
    ['hello', 'b', 'y', 'e']

    You have to use list.append(str)
     
  5. OP
    OP
    neRok

    neRok Member

    Joined:
    Aug 19, 2006
    Messages:
    4,166
    Location:
    Perth NOR
    Yes, but I added [string], ie a list of the string. I am adding a list to a list.

    Here is the little bit of code I am working on
    Code:
        special_filenames = []
        
        for filename in filenames:
          match = re.search(r'__\w+__', filename)
          if match: special_filenames += [filename]
     
    Last edited: May 28, 2013
  6. joe_sixpack

    joe_sixpack Member

    Joined:
    Jan 21, 2002
    Messages:
    2,850
    Location:
    Brisbane
    I'm not sure where in your original question you asked about adding two lists together, but you use '+=' to append a list with another list.

    If you use list.append(list2) you would end up with
    ['hello', ['bye']]
     
  7. OP
    OP
    neRok

    neRok Member

    Joined:
    Aug 19, 2006
    Messages:
    4,166
    Location:
    Perth NOR
    So which is faster? Appending string to a big list, or adding a small list (content a single string) to a big list? Both seem to get the same end result.

    list = ['Hello']
    string = 'World'

    list.append(string)

    OR

    list += [string]

    Both should give ['Hello', 'World']
     
    Last edited: May 28, 2013
  8. joe_sixpack

    joe_sixpack Member

    Joined:
    Jan 21, 2002
    Messages:
    2,850
    Location:
    Brisbane
    I think this boils down to what is the most efficient way to collect data into those lists to begin with..
     
  9. Taceo Corpus

    Taceo Corpus Member

    Joined:
    Sep 19, 2005
    Messages:
    3,253
    Location:
    Tokyo
    A tip for when you start playing around with even more languages: Each language is its own unique little snowflake. Trying to shoehorn your preferred paradigms into places they don't fit (like what you're trying to do here) will result in you writing buggy-ass code that is hard to debug and maintain. When working with a new language, leave preference at the door and follow best practices, they exist for a reason.
     
  10. cyclonite

    cyclonite Member

    Joined:
    Mar 7, 2002
    Messages:
    824
    Location:
    NSW 2009
    A cleaner way to write that would be to:

    Code:
    filename_re = re.compile(r'__\w+__')
    special_filenames = filter(lambda filename: filename_re.match(filename), filenames)
    
     
  11. Thhhza

    Thhhza Member

    Joined:
    Feb 5, 2003
    Messages:
    1,516
    Location:
    Melbourne
    +1 to cyclonite's solution.

    Back to the op's question, I would much prefer to see append() and extend() over += as it is explicit to what you are doing.
     
  12. OP
    OP
    neRok

    neRok Member

    Joined:
    Aug 19, 2006
    Messages:
    4,166
    Location:
    Perth NOR
    -1 to cyclonite's solution :p There have been no commands like that covered in the tutorial yet...

    I was working on another exercise and the append method actually made more sense, so will stick with it.
     

Share This Page

Advertisement: