Python Forum
Still learning - code efficiency, which of these is better?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Still learning - code efficiency, which of these is better?
#2
The functions are so short that I would one-line both of them:
def both_ends(string):
    return string[:2] + string[-2:] if len(string) >= 2 else ""


def fix_start(string):
  return string[0] + string[1:].replace(string[0], "*")
An experienced python programmer would have no issue reading either of these.  As a beginner it would certainly be easier to understand separated into more lines.

This isn't really a question of efficiency, but rather of readability.
Reply


Messages In This Thread
RE: Still learning - code efficiency, which of these is better? - by Mekire - Oct-14-2017, 04:38 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Sad I'm stuck with the reinforcement learning function in my Code OskiLori 2 1,637 May-20-2023, 10:01 PM
Last Post: OskiLori
  Numpy Structure and Efficiency garynewport 2 742 Oct-19-2022, 10:11 PM
Last Post: paul18fr
  Efficiency with regard to nested conditionals or and statements Mark17 13 3,316 May-06-2022, 05:16 PM
Last Post: Mark17
  How to use vectorization instead of for loop to improve efficiency in python? PJLEMZ 4 2,476 Feb-06-2021, 09:45 AM
Last Post: paul18fr
  Translation of R Code to Python for Statistical Learning Course SterlingAesir 2 2,176 Aug-27-2020, 08:46 AM
Last Post: ndc85430
  learning to code python nhan 5 2,600 May-23-2020, 03:35 PM
Last Post: nhan
  Any suggestions to improve BuySell stock problem efficiency? mrapple2020 0 1,399 May-13-2020, 06:19 PM
Last Post: mrapple2020
  Learning python, stuck on some code. stanceworksv8 2 3,561 Apr-02-2019, 01:51 AM
Last Post: stanceworksv8
  Help improve code efficiency benbrown03 9 4,443 Feb-20-2019, 03:45 PM
Last Post: ichabod801
  Web Scraping efficiency improvement HiImNew 0 2,421 Jun-01-2018, 08:52 PM
Last Post: HiImNew

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020