-2

I need to multiply the contents of an array in Python with a For Loop, and its not working. Basically this is the code I need:

g = [["flat"] in range(200)]

I basically need 200 "flat"s stored in an array called g. Can anyone help?

I expected it to store 200 'flat's in the array g, but instead, when I tried to print the contents of the array, it just printed ["False"]

BeRT2me
  • 12,699
  • 2
  • 13
  • 31

1 Answers1

0
g = ['flat'] * 200

Output:

['flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat', 'flat']
Alderven
  • 7,569
  • 5
  • 26
  • 38
  • 1
    Of course, you don't need the loop at all. `g = ['flat'] * 200` – tripleee Aug 26 '23 at 08:06
  • Note that this only works as expected if the items are value types rather than reference types. – Ouroborus Aug 26 '23 at 08:13
  • @Ouroborus What are "value types" and "reference types"? – Kelly Bundy Aug 26 '23 at 10:46
  • @KellyBundy Good question, because something like that doesn't exist. The correct description would have been "mutable values" and "immutable values". – Matthias Aug 26 '23 at 11:28
  • @KellyBundy https://en.wikipedia.org/wiki/Value_type_and_reference_type – Ouroborus Aug 27 '23 at 01:47
  • @Ouroborus Now look on that page in the "Classification per language" table for Python and you'll see no value types whatsoever. – Kelly Bundy Aug 27 '23 at 02:09
  • @KellyBundy Is there some distinction that is generally important in practice? – Ouroborus Aug 27 '23 at 04:37
  • 1
    @Ouroborus As Matthias said, you probably meant the issue of mutable vs immutable. Immutables like strings are safe to repeat this way, while repeating mutables like lists often causes problems (almost every day there's a new duplicate question about that on SO). – Kelly Bundy Aug 27 '23 at 06:08