Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
list not working
#1
    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?
Reply
#2
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)
Reply
#3
Thanks, it worked

I'll keep the naming in mind next time Smile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  os.list dir not working Kristenl2784 8 10,042 Jul-29-2020, 04:22 PM
Last Post: deanhystad
  python3: iterating through list not working wardancer84 3 2,367 Jul-08-2020, 04:30 PM
Last Post: DPaul
  Finding MINIMUM number in a random list is not working Mona 5 3,051 Nov-18-2019, 07:27 PM
Last Post: ThomasL
  Appending to list not working and causing a infinite loop eiger23 8 4,000 Oct-10-2019, 03:41 PM
Last Post: eiger23

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020