Python Forum

Full Version: list not working
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
    from __future__ import division
    import time, math

    List = [3,4,6]

    MADlist = List
    for i in range(len(MADlist)):
      MADlist[i] -= mean
      MADlist[i] = math.fabs(MADlist[i])
    MADsum = float(sum(MADlist))
    MAD = MADsum/(len(MADlist))
Here's my code.

I have a list called List. I want to make some changes without actually changing List, so I create another list, MADlist, to make changes to while keeping the original list the same. For whatever reason, it makes the same changes to the original list too! Why?
Never name a list List it's not a good idea.
It's like naming your baby 'human'.
The MADList is a pointer or reference to List, it's not a copy of list.
You can do it this way:
MADLList = []
for item in List:
    MADList.append(item)
Thanks, it worked

I'll keep the naming in mind next time Smile