By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,949 Members | 889 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,949 IT Pros & Developers. It's quick & easy.

Is there a more efficient way to code this project?

P: 1
I'm learning Python programming from the Mindmajix training and I just completed classes about lists, tuples, functions and methods. One of the projects following this lecture is to create a program which takes in a list as an input and outputs a corresponding string, as the following example showcases:

Expand|Select|Wrap|Line Numbers
  1. Input: ['bikes', 'cars', 'buses', 'trucks']
Output: bikes, cars, buses, and trucks

So I wrote the code like the below source code:

Expand|Select|Wrap|Line Numbers
  1. def listToString(someList):
  2.     for i in range(len(someList)):
  3.         newString = str(someList[i])
  4.  
  5.         if i == (len(someList)-1):
  6.             print('and ' + str(someList[i]))
  7.         else:
  8.             print(newString, end=', ')
  9.  
  10. someList = ['bikes','cars','buses','trucks']
  11. listToString(someList)
I feel like I didn't use everything (for example, some methods) to solve the problem. Is there a more efficient way to code this project?
Sep 25 '18 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.