title: list append not working correctly
body: <p>I have code that is supposed to fill in IP address ranges so that none overlap. I have had a previous version of this that works great but I changed it automate all the passes and be less wasteful and resource intensive.</p>
  
  <p>This is my current code:</p>
  
  <pre><code>import ipaddress
  import csv
  from csv import DictReader, DictWriter
  import cProfile
  import pstats
  import operator
  import time
  
  start_time=time.strftime("%I:%M:%S %p", time.localtime())
  
  
  alphlst=[]
  SubnetDic1=[]
  global SubnetDic2
  
  with open(r'C:\ExampleBefore.csv', newline='') as fin:
  
      read = DictReader(fin)
  
      for line in read:
          alphlst.append(line)
  
  
  
  def program():
      print("started")
      global alphlst
      lst3=[]
      global lst4
      lst4=[]
      global lst5
      lst5=[]
      endlist=[]
      global SubnetDic1
      SubnetDic1=[]
      global SubnetDic2
      SubnetDic2=[]
      global SubOut1
      SubOut1=[]
      global SubOut2
      SubOut2=[]
      msklst=[]
      global lst
      lst=[]
  
      print(alphlst)
  
      print("^ alphlst")
  
  
      for li in alphlst:
          li['CIDR']=ipaddress.ip_network(li['CIDR'])
          SubnetDic2.append(li)
          if str(li['CIDR'].netmask) != '255.255.255.255':
              SubnetDic1.append(li)
  
      print("alph list clear")
  
  
      alphlst=[]
  
      print("after alph list clear")
  
  
      print("Dic lists completed")
  
      def Overlap(y, z):
  
          global lst5
          global lst
  
          try:
              if y['CIDR'].overlaps(z['CIDR']):
                  lst=list(y['CIDR'].address_exclude(z['CIDR'])) # List of subnets excluding line4 subnet
                  if len(lst) != 0:
                      print("not empty. {} is obsolete.".format(y['CIDR']))
                      lst5.append(str(y['CIDR']))
                      print(lst)
          except ValueError:
              return False
  
  
  
  
  
      for line3 in SubnetDic1:
          line3['CIDR']=ipaddress.ip_network(line3['CIDR']) # Convert IP ranges to ip network object
          lst3.append(str(line3['CIDR']))
          for line4 in SubnetDic2:
              line4['CIDR']=ipaddress.ip_network(line4['CIDR']) # Convert IP ranges to ip network object
              Overlap(line3, line4) # Tests for IP overlap
              for val in lst:
                  if str(val) not in lst3:
                      if str(val) not in lst4:
                          subwr=line3
                          subwr['CIDR']=val # New range
                          subwr['IPStart']=val.network_address #New net address
                          subwr['IPStop']=val.broadcast_address #New broadcast address
                          SubOut1.append(subwr)
                          print("{} is line3".format(subwr))
                          print("{} is line3 tst".format(line3))
                          lst4.append(str(val))
  
  
  
      print(SubOut1)
      print("^ SubOut1")
  
  
  
  
      print("Subnet part1 completed")
  
      global lst7
  
  
      lst7=[]
  
  
      for line5 in SubnetDic2:
          line5['CIDR']=ipaddress.ip_network(line5['CIDR']) # Convert IP ranges to ip network object
          lst7.append(str(line5['CIDR']))
          print("{} line5 end".format(line5))
          if str(line5['CIDR']) not in lst5:
              print("{} line5 aft".format(line5))
              SubOut2.append(line5) # Write lines
  
      print("Subnet part2 completed")
  
  
      for line6 in SubOut2:
          endlist.append(line6)
  
      print("Subnet part3 completed")
  
      for line7 in SubOut1:
          endlist.append(line7)
  
      print("Subnet part4 completed")
  
      lst8=[]
  
      for line10 in endlist:
          line10['CIDR']=ipaddress.ip_network(line10['CIDR'])
          line10['Mask']=line10['CIDR'].netmask
          print("{} is line10 try".format(line10))
          if str(line10['CIDR']) not in lst8:
              msklst.append(line10)
              lst8.append(str(line10['CIDR']))
              print("{} is line10 append".format(line10))
  
  
  
      srtlst=sorted(msklst, key=operator.itemgetter('Mask'), reverse=True)
  
      for lip in srtlst:
          alphlst.append(lip)
  
  
  
  
  
  
  def all_true(items):
      return all(x == "True" for x in items)
  
  lstadd=[]
  
  def in_lst():
  
      global lst4
      global SubnetDic2
      global lstadd
      global lst7
  
      lstadd=[]
  
      for l in lst4:
          if l in lst7:
              lstadd.append("True")
          else:
              lstadd.append("False")
  
      print(lstadd)
      print(lst4)
  
  def chk_lst():
  
      print("running chk_lst")
  
      in_lst()
  
      global lstadd
  
      if all_true(lstadd):
          return True
      else:
          return False
  
  
  pass_start=time.strftime("%I:%M:%S %p", time.localtime())
  cnt=1
  cProfile.run('program()', 'prostats')
  pass_end=time.strftime("%I:%M:%S %p", time.localtime())
  p = pstats.Stats('prostats')
  p.print_stats(0)
  print("Pass {0}. Start: {1} End: {2}".format(cnt, pass_start, pass_end))
  
  
  while chk_lst() is False:
      pass_start2=time.strftime("%I:%M:%S %p", time.localtime())
      cnt = cnt + 1
      cProfile.run('program()', 'prostats2')
      pass_end2=time.strftime("%I:%M:%S %p", time.localtime())
      p = pstats.Stats('prostats2')
      p.print_stats(0)
      print("Pass {0}. Start: {1} End: {2}".format(cnt, pass_start2, pass_end2))
  
  
  print("All Subnets Done")    
  
  
  with open(r'C:\ExampleBefore.csv', newline='') as fin7,\
       open(r'C:\TstExample-sort1.csv', 'w', newline='') as fout5:
  
          read7 = DictReader(fin7)
  
          writenum6 = DictWriter(fout5, fieldnames=read7.fieldnames, extrasaction='ignore') 
          writenum6.writeheader()
  
          global alphlst
  
          for lime in alphlst:
             writenum6.writerow(lime)
  
          print("Write Complete")
  </code></pre>
  
  <p>I know it's messy and I know I am doing some unnecessary things in it right now, that is because I was trying things to make it work and haven't cleaned it up yet. All the printing, for example is for troubleshooting only.</p>
  
  <p>This is and example of the input file (the real one is much bigger though):</p>
  
  <pre><code>Zone Name,IPStart,IPStop,CIDR,Source
  Group A - Server,10.55.22.1/32,10.55.22.1/32,10.55.22.1/32,New List
  Group A Sales Primary routers,10.23.1.0,10.23.1.15,10.24.1.0/28,New List
  Group A Sales Web Servers - Primary,10.18.0.0,10.18.0.255,10.18.0.0/24,New List
  Group A Marketing,10.62.0.0,10.62.255.255,10.62.0.0/16,New List
  Group A Research,10.62.0.0,10.63.255.255,10.62.0.0/15,Old List
  Group A Sales Web Servers,10.16.0.0,10.19.255.255,10.16.0.0/14,New List
  Group A Sales,10.16.0.0,10.31.255.255,10.16.0.0/12,New List
  Group A,10.0.0.0,10.127.255.255,10.0.0.0/9,New List
  Group B,10.128.0.0,10.255.255.255,10.128.0.0/9,Old List
  </code></pre>
  
  <p>I have figured out that the problem is in this part of the code:</p>
  
  <pre><code>for line3 in SubnetDic1:
          line3['CIDR']=ipaddress.ip_network(line3['CIDR']) # Convert IP ranges to ip network object
          lst3.append(str(line3['CIDR']))
          for line4 in SubnetDic2:
              line4['CIDR']=ipaddress.ip_network(line4['CIDR']) # Convert IP ranges to ip network object
              Overlap(line3, line4) # Tests for IP overlap
              for val in lst:
                  if str(val) not in lst3:
                      if str(val) not in lst4:
                          subwr=line3
                          subwr['CIDR']=val # New range
                          subwr['IPStart']=val.network_address #New net address
                          subwr['IPStop']=val.broadcast_address #New broadcast address
                          SubOut1.append(subwr)
                          print("{} is line3".format(subwr))
                          print("{} is line3 tst".format(line3))
                          lst4.append(str(val))
  
  
  
      print(SubOut1)
      print("^ SubOut1")
  </code></pre>
  
  <p>Basically, the value being appended to <code>SubOut1</code> does not match what is supposed to be. It is however printing what it is supposed to be in both of the lines <code>print("{} is line3".format(subwr))</code> and <code>print("{} is line3 tst".format(line3))</code>. Keep in mind I was using "line3" before as the value to append to the list instead of <code>subwr</code> but that produced the same result.</p>
  
  <p>This is an example of what it prints for the lines <code>print("{} is line3".format(subwr))</code> and <code>print("{} is line3 tst".format(line3))</code>:</p>
  
  <pre><code>{'Zone Name': 'Group A', 'IPStart': IPv4Address('10.64.0.0'), 'CIDR': IPv4Network('10.64.0.0/10'), 'Source': 'New List', 'IPStop': IPv4Address('10.127.255.255')} is line3
  {'Zone Name': 'Group A', 'IPStart': IPv4Address('10.64.0.0'), 'CIDR': IPv4Network('10.64.0.0/10'), 'Source': 'New List', 'IPStop': IPv4Address('10.127.255.255')} is line3 tst
  </code></pre>
  
  <p>And this is what is in the list "SubOut1" the first pass (when it SHOULD have the line above in it and others):</p>
  
  <pre><code>[{'Zone Name': 'Group A Research', 'IPStart': IPv4Address('10.63.0.0'), 'CIDR': IPv4Network('10.63.0.0/16'), 'Source': 'Old List', 'IPStop': IPv4Address('10.63.255.255')}, {'Zone Name': 'Group A Sales Web Servers', 'IPStart': IPv4Address('10.18.1.0'), 'CIDR': IPv4Network('10.18.1.0/24'), 'Source': 'New List', 'IPStop': IPv4Address('10.18.1.255')}, {'Zone Name': 'Group A Sales Web Servers', 'IPStart': IPv4Address('10.18.1.0'), 'CIDR': IPv4Network('10.18.1.0/24'), 'Source': 'New List', 'IPStop': IPv4Address('10.18.1.255')}, {'Zone Name': 'Group A Sales Web Servers', 'IPStart': IPv4Address('10.18.1.0'), 'CIDR': IPv4Network('10.18.1.0/24'), 'Source': 'New List', 'IPStop': IPv4Address('10.18.1.255')}, {'Zone Name': 'Group A Sales Web Servers', 'IPStart': IPv4Address('10.18.1.0'), 'CIDR': IPv4Network('10.18.1.0/24'), 'Source': 'New List', 'IPStop': IPv4Address('10.18.1.255')}, {'Zone Name': 'Group A Sales Web Servers', 'IPStart': IPv4Address('10.18.1.0'), 'CIDR': IPv4Network('10.18.1.0/24'), 'Source': 'New List', 'IPStop': IPv4Address('10.18.1.255')}, {'Zone Name': 'Group A Sales Web Servers', 'IPStart': IPv4Address('10.18.1.0'), 'CIDR': IPv4Network('10.18.1.0/24'), 'Source': 'New List', 'IPStop': IPv4Address('10.18.1.255')}, {'Zone Name': 'Group A Sales Web Servers', 'IPStart': IPv4Address('10.18.1.0'), 'CIDR': IPv4Network('10.18.1.0/24'), 'Source': 'New List', 'IPStop': IPv4Address('10.18.1.255')}, {'Zone Name': 'Group A Sales Web Servers', 'IPStart': IPv4Address('10.18.1.0'), 'CIDR': IPv4Network('10.18.1.0/24'), 'Source': 'New List', 'IPStop': IPv4Address('10.18.1.255')}, {'Zone Name': 'Group A Sales Web Servers', 'IPStart': IPv4Address('10.18.1.0'), 'CIDR': IPv4Network('10.18.1.0/24'), 'Source': 'New List', 'IPStop': IPv4Address('10.18.1.255')}, {'Zone Name': 'Group A Sales Web Servers', 'IPStart': IPv4Address('10.18.1.0'), 'CIDR': IPv4Network('10.18.1.0/24'), 'Source': 'New List', 'IPStop': IPv4Address('10.18.1.255')}, {'Zone Name': 'Group A Sales', 'IPStart': IPv4Address('10.24.1.16'), 'CIDR': IPv4Network('10.24.1.16/28'), 'Source': 'New List', 'IPStop': IPv4Address('10.24.1.31')}, {'Zone Name': 'Group A Sales', 'IPStart': IPv4Address('10.24.1.16'), 'CIDR': IPv4Network('10.24.1.16/28'), 'Source': 'New List', 'IPStop': IPv4Address('10.24.1.31')}, {'Zone Name': 'Group A Sales', 'IPStart': IPv4Address('10.24.1.16'), 'CIDR': IPv4Network('10.24.1.16/28'), 'Source': 'New List', 'IPStop': IPv4Address('10.24.1.31')}, {'Zone Name': 'Group A Sales', 'IPStart': IPv4Address('10.24.1.16'), 'CIDR': IPv4Network('10.24.1.16/28'), 'Source': 'New List', 'IPStop': IPv4Address('10.24.1.31')}, {'Zone Name': 'Group A Sales', 'IPStart': IPv4Address('10.24.1.16'), 'CIDR': IPv4Network('10.24.1.16/28'), 'Source': 'New List', 'IPStop': IPv4Address('10.24.1.31')}, {'Zone Name': 'Group A Sales', 'IPStart': IPv4Address('10.24.1.16'), 'CIDR': IPv4Network('10.24.1.16/28'), 'Source': 'New List', 'IPStop': IPv4Address('10.24.1.31')}, {'Zone Name': 'Group A Sales', 'IPStart': IPv4Address('10.24.1.16'), 'CIDR': IPv4Network('10.24.1.16/28'), 'Source': 'New List', 'IPStop': IPv4Address('10.24.1.31')}, {'Zone Name': 'Group A Sales', 'IPStart': IPv4Address('10.24.1.16'), 'CIDR': IPv4Network('10.24.1.16/28'), 'Source': 'New List', 'IPStop': IPv4Address('10.24.1.31')}, {'Zone Name': 'Group A Sales', 'IPStart': IPv4Address('10.24.1.16'), 'CIDR': IPv4Network('10.24.1.16/28'), 'Source': 'New List', 'IPStop': IPv4Address('10.24.1.31')}, {'Zone Name': 'Group A Sales', 'IPStart': IPv4Address('10.24.1.16'), 'CIDR': IPv4Network('10.24.1.16/28'), 'Source': 'New List', 'IPStop': IPv4Address('10.24.1.31')}, {'Zone Name': 'Group A Sales', 'IPStart': IPv4Address('10.24.1.16'), 'CIDR': IPv4Network('10.24.1.16/28'), 'Source': 'New List', 'IPStop': IPv4Address('10.24.1.31')}, {'Zone Name': 'Group A Sales', 'IPStart': IPv4Address('10.24.1.16'), 'CIDR': IPv4Network('10.24.1.16/28'), 'Source': 'New List', 'IPStop': IPv4Address('10.24.1.31')}, {'Zone Name': 'Group A Sales', 'IPStart': IPv4Address('10.24.1.16'), 'CIDR': IPv4Network('10.24.1.16/28'), 'Source': 'New List', 'IPStop': IPv4Address('10.24.1.31')}, {'Zone Name': 'Group A Sales', 'IPStart': IPv4Address('10.24.1.16'), 'CIDR': IPv4Network('10.24.1.16/28'), 'Source': 'New List', 'IPStop': IPv4Address('10.24.1.31')}, {'Zone Name': 'Group A Sales', 'IPStart': IPv4Address('10.24.1.16'), 'CIDR': IPv4Network('10.24.1.16/28'), 'Source': 'New List', 'IPStop': IPv4Address('10.24.1.31')}, {'Zone Name': 'Group A Sales', 'IPStart': IPv4Address('10.24.1.16'), 'CIDR': IPv4Network('10.24.1.16/28'), 'Source': 'New List', 'IPStop': IPv4Address('10.24.1.31')}, {'Zone Name': 'Group A', 'IPStart': IPv4Address('10.55.22.0'), 'CIDR': IPv4Network('10.55.22.0/32'), 'Source': 'New List', 'IPStop': IPv4Address('10.55.22.0')}, {'Zone Name': 'Group A', 'IPStart': IPv4Address('10.55.22.0'), 'CIDR': IPv4Network('10.55.22.0/32'), 'Source': 'New List', 'IPStop': IPv4Address('10.55.22.0')}, {'Zone Name': 'Group A', 'IPStart': IPv4Address('10.55.22.0'), 'CIDR': IPv4Network('10.55.22.0/32'), 'Source': 'New List', 'IPStop': IPv4Address('10.55.22.0')}, {'Zone Name': 'Group A', 'IPStart': IPv4Address('10.55.22.0'), 'CIDR': IPv4Network('10.55.22.0/32'), 'Source': 'New List', 'IPStop': IPv4Address('10.55.22.0')}, {'Zone Name': 'Group A', 'IPStart': IPv4Address('10.55.22.0'), 'CIDR': IPv4Network('10.55.22.0/32'), 'Source': 'New List', 'IPStop': IPv4Address('10.55.22.0')}, {'Zone Name': 'Group A', 'IPStart': IPv4Address('10.55.22.0'), 'CIDR': IPv4Network('10.55.22.0/32'), 'Source': 'New List', 'IPStop': IPv4Address('10.55.22.0')}, {'Zone Name': 'Group A', 'IPStart': IPv4Address('10.55.22.0'), 'CIDR': IPv4Network('10.55.22.0/32'), 'Source': 'New List', 'IPStop': IPv4Address('10.55.22.0')}, {'Zone Name': 'Group A', 'IPStart': IPv4Address('10.55.22.0'), 'CIDR': IPv4Network('10.55.22.0/32'), 'Source': 'New List', 'IPStop': IPv4Address('10.55.22.0')}, {'Zone Name': 'Group A', 'IPStart': IPv4Address('10.55.22.0'), 'CIDR': IPv4Network('10.55.22.0/32'), 'Source': 'New List', 'IPStop': IPv4Address('10.55.22.0')}, {'Zone Name': 'Group A', 'IPStart': IPv4Address('10.55.22.0'), 'CIDR': IPv4Network('10.55.22.0/32'), 'Source': 'New List', 'IPStop': IPv4Address('10.55.22.0')}, {'Zone Name': 'Group A', 'IPStart': IPv4Address('10.55.22.0'), 'CIDR': IPv4Network('10.55.22.0/32'), 'Source': 'New List', 'IPStop': IPv4Address('10.55.22.0')}, {'Zone Name': 'Group A', 'IPStart': IPv4Address('10.55.22.0'), 'CIDR': IPv4Network('10.55.22.0/32'), 'Source': 'New List', 'IPStop': IPv4Address('10.55.22.0')}, {'Zone Name': 'Group A', 'IPStart': IPv4Address('10.55.22.0'), 'CIDR': IPv4Network('10.55.22.0/32'), 'Source': 'New List', 'IPStop': IPv4Address('10.55.22.0')}, {'Zone Name': 'Group A', 'IPStart': IPv4Address('10.55.22.0'), 'CIDR': IPv4Network('10.55.22.0/32'), 'Source': 'New List', 'IPStop': IPv4Address('10.55.22.0')}, {'Zone Name': 'Group A', 'IPStart': IPv4Address('10.55.22.0'), 'CIDR': IPv4Network('10.55.22.0/32'), 'Source': 'New List', 'IPStop': IPv4Address('10.55.22.0')}, {'Zone Name': 'Group A', 'IPStart': IPv4Address('10.55.22.0'), 'CIDR': IPv4Network('10.55.22.0/32'), 'Source': 'New List', 'IPStop': IPv4Address('10.55.22.0')}, {'Zone Name': 'Group A', 'IPStart': IPv4Address('10.55.22.0'), 'CIDR': IPv4Network('10.55.22.0/32'), 'Source': 'New List', 'IPStop': IPv4Address('10.55.22.0')}, {'Zone Name': 'Group A', 'IPStart': IPv4Address('10.55.22.0'), 'CIDR': IPv4Network('10.55.22.0/32'), 'Source': 'New List', 'IPStop': IPv4Address('10.55.22.0')}, {'Zone Name': 'Group A', 'IPStart': IPv4Address('10.55.22.0'), 'CIDR': IPv4Network('10.55.22.0/32'), 'Source': 'New List', 'IPStop': IPv4Address('10.55.22.0')}, {'Zone Name': 'Group A', 'IPStart': IPv4Address('10.55.22.0'), 'CIDR': IPv4Network('10.55.22.0/32'), 'Source': 'New List', 'IPStop': IPv4Address('10.55.22.0')}, {'Zone Name': 'Group A', 'IPStart': IPv4Address('10.55.22.0'), 'CIDR': IPv4Network('10.55.22.0/32'), 'Source': 'New List', 'IPStop': IPv4Address('10.55.22.0')}, {'Zone Name': 'Group A', 'IPStart': IPv4Address('10.55.22.0'), 'CIDR': IPv4Network('10.55.22.0/32'), 'Source': 'New List', 'IPStop': IPv4Address('10.55.22.0')}, {'Zone Name': 'Group A', 'IPStart': IPv4Address('10.55.22.0'), 'CIDR': IPv4Network('10.55.22.0/32'), 'Source': 'New List', 'IPStop': IPv4Address('10.55.22.0')}]
  </code></pre>
  
  <p>The code that worked before used the DictWriter to write row instead of appending to a list. It was "write.writerow(line3)" instead of what it is now <code>SubOut1.append(subwr)</code> or the same thing: <code>SubOut1.append(line3)</code>.</p>
  
  <p>Can someone please help me figure out why it is not appending the correct values to <code>SubOut1</code> and how to fix it?</p>
  
  <p>Thanks</p>