2

let var be my variable. I want to create a list like this :

new_list=[var,var,var,var,var,var,var,var]

how to do it (not manually, obviously) ?

6
  • 3
    What's inside var? A mutable or an immutable object? Commented Oct 23, 2013 at 21:30
  • 1
    @TimPietzcker Indeed... I was trying to think of a simple way to ask... The best I've come up with is: "Do you want the same object repeated eight times, or eight copies of the object"... So ugh... Commented Oct 23, 2013 at 21:32
  • @TimPietzcker it can be an integer, a string,etc... any object. Commented Oct 23, 2013 at 21:32
  • can it be a list? A dict? Those could cause you problems. (hence Tim/Jon asking about mutability) Commented Oct 23, 2013 at 21:37
  • @user2913243 integer or string are fine (they are immutable) but lists, dicts, user defined objects may cause you to have issues later if you change them and expect them to not all change. Commented Oct 23, 2013 at 21:37

3 Answers 3

5

You can do it with multiplication operator:

new_list = [var] * 8

Also remember that variables store references to objects in Python, so:

ob = MyObject()
new_list = [ob] * 8

will create a list consisting of 8 references to the same object. If you change it - all of them change.

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

6 Comments

or new_list = [var for i in range(8)]
Let's just hope that var is not a mutable object, or the OP is in for a nasty surprise.
@TimPietzcker, thought of that after pushed Answer, already published additional explanation ;)
@RostyslavDzinko and that is no an english sentence. It is an answer, it will create a list with 8 or any number n instances of var is.
@abarnert My comment was referring to an unnecessary comment that he has since deleted. The italicized no was a play on the fact that he wrote "That is no an answer to the question asked...", when in fact it is an answer to the question asked, I did not post it as an answer because I felt it was more of an add-on to his answer than being worth posting separately as they are both valid, hence my original use of the word "or" in my first comment. He instead felt the need to be petty and so by the rules of the internet, I attacked his command of the english language.
|
2

You can use list comprehension syntax:

 new_list=[var for i in range(8)]

3 Comments

why would you do this instead of the other solution? I cant think of any reason, but maybe I am overlooking something (I could see the point if you were using copy.copy or something)
convention is _ instead of i if its unused, but other then that, I like this way better
If var were an expression that evaluated to a new copy each time (like [], or spam.make_eggs() or even, as Joran suggests, copy(var) or var[:]), this would be the right solution (assuming you wanted 8 new copies instead of the same object 8 times). Otherwise, it's just misleading—it seems like it's avoiding the * 8 trap, but it really isn't.
1

You can also use itertools.repeat to do that:

>>> from itertools import repeat
>>> repeatvars = repeat('var', 8)
>>> repeatvars
repeat('var', 8)
>>> list(repeatvars)
['var', 'var', 'var', 'var', 'var', 'var', 'var', 'var']

and there are many useful functions under the itertools module, you can learn it from this site, hope that will help.

Comments

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.