1

Wondering what below code mean? Does foo and goo initialized as a dictionary?

foo = [0] * 128
goo = [False] * 128
1
  • In all seriousness, this is when the interactive Python interpreter helps out a lot. If you say foo = [0] * 5 ; foo it will dutifully reply [0, 0, 0, 0, 0]. If you say type(foo) it will respond <class 'list'>, and so on. Commented Sep 12, 2015 at 2:52

1 Answer 1

3

Multiplying a list by an integer N creates a new list with the contents of the original list repeated N times.

[123] * 4 = [123, 123, 123, 123].

So [0] * 128 gives a 128-item list where each item is 0.

[False] * 128 gives a 128-item list where each item is False.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks! Is there a way to know if [123] * 4 is a list type object?
Yes, [123] is a sequence of one element so * int can be applied yielding [123, 123, 123, 123].
@msw you can multiply a list of any length by an integer. [1, 2] * 3 = [1, 2, 1, 2, 1, 2].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.