-1

Is there a dynamic way (without resorting to a loop or using the fixed []) of creating a Python list with N elements?

The elements can be anything e.g. strings, numbers, characters, etc. For example if N = 6, how can I dynamically create a list that has 6 string elements "abc"?

["abc", "abc", "abc", "abc", "abc","abc"]

This should be dynamic for different values of N and the list elements.

jonrsharpe
  • 107,083
  • 22
  • 201
  • 376

1 Answers1

2

The pythonic way to do this is:

["abc"]*6

As jonsharpe commented, if you elements are mutable, this will create elements with the same reference. To create a new object in each case, you could do

[[] for _ in range(6)]
simonzack
  • 17,855
  • 12
  • 65
  • 107