468,291 Members | 1,723 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,291 developers. It's quick & easy.

Is there a more efficient way to code this project?

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
0 714

Post your reply

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

Similar topics

1 post views Thread by Howie | last post: by
reply views Thread by IlQlo | last post: by
19 posts views Thread by aurgathor | last post: by
13 posts views Thread by gonzlobo | last post: by
eWish
10 posts views Thread by eWish | last post: by
reply views Thread by NPC403 | last post: by
2 posts views Thread by MrBee | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.