0

My apology for this most inane question. I have very basic python knowledge, and I'm working on it. I need to create a simply list in python that would take a function call with two arguments createList(v,n):

So that value would be a string say e and the next argument would be a number 5 and that would then create a list-

['e','e','e','e','e','e']

Conceptually I understand that e would be at index 0 and 5 would be at index 1, but I have no idea how to actually use the arguments to make the list.

I have searched to try and learn but there is nothing so simply as this. Again, my apology for the inane question, but I am trying!

Rory Daulton
  • 21,934
  • 6
  • 42
  • 50
MGinOttawa
  • 11
  • 1
  • Welcome to the site! Check out the [tour](https://stackoverflow.com/tour) and the [how-to-ask page](https://stackoverflow.com/help/how-to-ask) for more about asking questions that will attract quality answers. You can [edit your question](https://stackoverflow.com/posts/49466525/edit) to include more information. – cxw Mar 24 '18 at 15:37
  • 1
    Possible duplicate of [Create List of Single Item Repeated n Times in Python](https://stackoverflow.com/questions/3459098/create-list-of-single-item-repeated-n-times-in-python) – Michael Kohl Mar 24 '18 at 15:37

4 Answers4

4
def createList(v,n):
    return [v] * n
  • You use the arguments by simply referring to them by name :) . No need to worry about which argument is first or second when you are inside createList — though they do need to be in the right order when you call createList.
  • [v] makes a one-element list that contains v.
  • * n, when applied to lists, expands that list to include n copies of what it had before.
  • return returns the value from the function.
cxw
  • 16,685
  • 2
  • 45
  • 81
  • It says not to say thank you, but I am going to do so anyway, what simple syntax. I was conceptualizing things as far more complex. Thank you very much. – MGinOttawa Mar 24 '18 at 15:47
  • @MGinOttawa No worries :) . Upvote/accept (checkmark) are the encouraged ways to say thanks, but I can't imagine anyone would object to a personal message :) . – cxw Mar 24 '18 at 15:51
  • @MGinOttawa And, as you are discovering, you can only hit the checkmark on one answer - preferably, the one that best helped you in your situation. You can, however, upvote as many answers as you want. – cxw Mar 24 '18 at 15:51
1
def createList(v,n): 
    return [v for i in range(n)]

This solution is based on list comprehension.

Michael Kohl
  • 66,324
  • 14
  • 138
  • 158
Muthuraj
  • 41
  • 8
  • This is a nice solution and again simple and elegant, thank you. I was thinking it would be more complicated such as find the value of index 0, then find the value of index 1 and create a loop that would go through and insert index 0 the number of times as index 1. – MGinOttawa Mar 24 '18 at 15:51
0
def createList(v,n):
    print([v for i in range(n)])

You can learn function and arguments in

https://www.tutorialspoint.com/python/python_functions.htm

Syed Ali
  • 219
  • 3
  • 11
0

Slight improved answered of @cxw. Which will work for almost all the case except when n = 0.

def createList(v,n):
    return [v] * n if (n != 0) else [v]

Since if n=0 the list [v] * 0 would become empty [] list which I think wont be expected result in your case.

MaNKuR
  • 2,578
  • 1
  • 19
  • 31