1
\$\begingroup\$

I'm a beginner python programmer..I recently started reading a python book called: "Python Crash Course, 2nd Edition: A Hands-On, Project-Based Introduction to Programming"...And I found an interesting challenge...The challenge is to copy the elements of the list to another list and change the elements to make them become lowercase...I solved the challenge by myself and wrote two versions for it...

Here is my first attempt:

#Method1: Using the slice method
currentUsers = ['MrSplash', 'MrRobot', 'Peter1234', 'Steve', 'James']
newUsers = ['Davidpeterson', 'Elliot345', 'STEVE', 'mrRobOt', 'NickB3ns0N']

currentUsersLower = currentUsers[:]
for i in range(0, 5):
    currentUsersLower[i] = currentUsersLower[i].lower()

And here is my second attempt:

#Method2: Using a normal for loop
currentUsers = ['MrSplash', 'MrRobot', 'Peter1234', 'Steve', 'James']
newUsers = ['Davidpeterson', 'Elliot345', 'STEVE', 'mrRobOt', 'NickB3ns0N']

currentUsersLower = []
for user in currentUsers:
    currentUsersLower.append(user.lower())

My first question is this: Imagine we are working with around a million elements here. Which of these methods is efficient?

I define efficient as:

  • Easy to maintain and read
  • Performance-wise(Because let's face it, a million elements requires a lot of performance here)

My second question is this: Are there better ways to do this task than the ones I presented here? If so please share them as an answer here

Note: The code works on both Python 3 and 2.

\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

Your second version is better than your first version, for two reasons:

  1. All else being equal, building a new list is IMO easier to reason about than changing a list you're iterating over.
  2. You're iterating using for...in rather than iterating by index using a range (and the range in your first example is hardcoded rather than being over the len of the list, which is doubly bad).

The most efficient/pythonic way is to use the comprehension syntax:

currentUsersLower = [user.lower() for user in currentUsers]

(Also, standard Python style would be to name these current_users and current_users_lower!)

\$\endgroup\$
3
  • \$\begingroup\$ Thank you for your answer...I really like the list comprehension method but at the time I was programming, I totally forgot about this method too(LOL...I guess I was tired)...But one thing...Is it actually bad to use camel case method for naming variables and lists here?(I'm asking because I want to make sure If it's a bad practice or not) \$\endgroup\$ Commented Apr 28, 2020 at 15:26
  • 2
    \$\begingroup\$ It's not "bad" in terms of making your code not work, but it will make it not look like most other Python code, and almost everyone who reviews your code will comment on it. Best to just drink the kool-aid. :) \$\endgroup\$
    – Samwise
    Commented Apr 28, 2020 at 15:30
  • 2
    \$\begingroup\$ See: python.org/dev/peps/pep-0008/#function-and-variable-names It's pretty common for Python devs to use a linter that will auto-correct and/or complain when you violate these style guidelines, so violations of them stand out like misspellings once you're thoroughly indoctrinated. \$\endgroup\$
    – Samwise
    Commented Apr 28, 2020 at 15:32
1
\$\begingroup\$

A1) Apart from efficiency issues, neither of these methods are pythonic from the Python point of view. But, If I had to choose one, I would choose the second one. Because, First is not general way in python.

A2) I think it would be better to use List comprehension because it is the most efficient in terms of memory performance. You can check it out on this site

In the case of me,

# Using list comprehension

In [1]: current_users = ['MrSplash', 'MrRobot', 'Peter1234', 'Steve', 'James']

In [2]: current_users_lower = [user.lower() for user in current_users]

In [3]: print(current_users_lower)
['mrsplash', 'mrrobot', 'peter1234', 'steve', 'james']
# Another way is to use map

In [5]: current_users_lower = list(map(str.lower, current_users))

In [6]: print(current_users_lower)
['mrsplash', 'mrrobot', 'peter1234', 'steve', 'james']

I think it would be written as above.

cc. python official convention about variable is snake_case not camelCase (I recommend to read PEP8 which is python official style guide)

Happy Coding~

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.