-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
  • 115,751
  • 26
  • 228
  • 437

1 Answers1

3

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
  • 19,729
  • 13
  • 73
  • 118