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.