[Tutor] How can I replicate a list in a for loop
Mark Lawrence
breamoreboy at yahoo.co.uk
Wed Feb 4 00:53:17 CET 2015
On 03/02/2015 22:51, Suresh Nagulavancha wrote:
> This is my sample piece of code (not full code of my original code just sample of it)
> I am using python 2.7
> Import itertools
> var=[1,2,3,4,5]
> Length=int(raw_input("length of possible numbers required"))
> #This is for length of 3 chars
> for i in itertools.product(var,var,var):
> print i[0]+i[1]+i[2]
>
> Here what is my problem is i am unable to replicate the list based on the user input i tried var*length but it is creating a new single list with repeated values , how can i solve this problem?? I want the same original list to be used
>
> Thanks in advance
If you are trying to get the first n items from the list all you need is
the slice notation:-
chunk = var[:Length]
If not can you please rephrase your question as it makes no sense to me.
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence
More information about the Tutor
mailing list