Take the 2-minute tour ×

I have a list of 100 elements. I am trying to create a function that will make 300 copies of that list and then store those copies into a blank list. I then need the function to choose an indexed value at random from each copied list. So, it might choose the 25th index value in the first copied list, and then it might choose the 60th index value in the next copied list. Then, the index of the value is an argument of a pre-defined function. The problem is that my copied lists are not being manipulated.

my code is as follows:

def condition_manipulate(value):
    list_set=[]                  #this is the list in which the copied lists will go
    for i in range(0,value):
        new_list=initial_conditions[:]    #initial_conditions is the list to be copied
        for i in list_set:           #My confusion is here. I need the function to choose
            for j in i:              #A random value in each copied list that resides
                 x=random.choice(i)  #In list_set and then run a predefined function on it.
    return list_set

#running condition_manipulate(300) should give me a list with 300 copies of a list
#Where a random value in each list is manipulated by the function new_sum

I have tried almost everything. What am I doing wrong? Any Help will be appreciated. Thanks.

share|improve this question
What's your end goal here? Is there a reason you need 300 copies of the same list? –  rogaos Jul 4 '13 at 18:37
we had the same question an hour ago lol –  Stephan Jul 4 '13 at 18:38
Why don't you choose three times from the same list? –  Markus Unterwaditzer Jul 4 '13 at 18:39
My immediate instinct is to say "why not create the lists as you need them, use them, then discard them?". I do understand that there are some situations where that won't work, but if you can do that, it will save a lot of work. I will see if I can give a real answer too. –  AMADANON Inc. Jul 4 '13 at 18:39
@Rogaos My end goal is to plot those 300 new lists on a graph. For every x value on the graph, a list is graphed. The list has either -1's or 1's, and if the index-value is -1, a red dot is plotted on the y axis. Example: The fifth item in the fifth list might be -1. So, a red dot will be plotted on x=5,y=5. Like this, each list will be plotted –  user2509830 Jul 4 '13 at 18:40
show 3 more comments

2 Answers

up vote 1 down vote accepted


import random

def condition_manipulate(value):
    for i in range(value):
    return list_set
share|improve this answer
Thank you SO MUCH. This code FINALLY WORKED. I truly appreciate your help! –  user2509830 Jul 4 '13 at 19:04
add comment

If you really need copies of lists rather than shallow copies then you need to:

import copy

oldlist = [.....]
newlist = copy.deepcopy(oldlist)

Otherwise all the copies are really the same list.>>> o = [1, 2, 3]

>>> n = o
>>> n.append(4)
>>> o
[1, 2, 3, 4]
>>> n = copy.deepcopy(o)
>>> n
[1, 2, 3, 4]
>>> n.append(5)
>>> n
[1, 2, 3, 4, 5]
>>> o
[1, 2, 3, 4]
share|improve this answer
add comment

Your Answer


