<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Python question -- List in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/python-question-list/m-p/1198936#M65162</link>
    <description>&lt;P&gt;For starters, you have some invalid code so I am not sure how you are testing.&amp;nbsp; For example,&lt;/P&gt;&lt;LI-CODE lang="python"&gt;&amp;gt;&amp;gt;&amp;gt; list2.insert[0, "id"]
Traceback (most recent call last):
  File "&amp;lt;stdin&amp;gt;", line 1, in &amp;lt;module&amp;gt;
TypeError: 'builtin_function_or_method' object is not subscriptable&lt;/LI-CODE&gt;&lt;P&gt;Changing the code to proper syntax, I get the expected results in Python 3.7.11 bundled with ArcGIS Pro 2.9.x&lt;/P&gt;&lt;LI-CODE lang="python"&gt;Python 3.7.11 [MSC v.1927 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
&amp;gt;&amp;gt;&amp;gt; list1=["v1", "v2"," v3"]
&amp;gt;&amp;gt;&amp;gt; list2=list1
&amp;gt;&amp;gt;&amp;gt; list2.insert(0, "id")
&amp;gt;&amp;gt;&amp;gt; list1
['id', 'v1', 'v2', ' v3']
&amp;gt;&amp;gt;&amp;gt; list2
['id', 'v1', 'v2', ' v3']
&amp;gt;&amp;gt;&amp;gt;&lt;/LI-CODE&gt;</description>
    <pubDate>Wed, 03 Aug 2022 14:43:22 GMT</pubDate>
    <dc:creator>JoshuaBixby</dc:creator>
    <dc:date>2022-08-03T14:43:22Z</dc:date>
    <item>
      <title>Python question -- List</title>
      <link>https://community.esri.com/t5/python-questions/python-question-list/m-p/1198932#M65160</link>
      <description>&lt;P&gt;I created a list variable and assigned another variable to it.&amp;nbsp; Then i modified the 2nd by adding a new item to it.&amp;nbsp; I found out the 1st variable changed too.&amp;nbsp; Just want to confirm that assignment does not create a new list.&lt;/P&gt;&lt;P&gt;list1=["v1", "v2"," v3"]&lt;/P&gt;&lt;P&gt;list2=list1&lt;/P&gt;&lt;P&gt;list2.insert[0, "id"]&lt;/P&gt;&lt;P&gt;so list2 output is:&amp;nbsp;["id", "v1", "v2"," v3"]&lt;/P&gt;&lt;P&gt;however list1 output is the same.&lt;/P&gt;&lt;P&gt;So i can't use assignment to create a new list from list1.&amp;nbsp; rather i need to do this:&lt;/P&gt;&lt;P&gt;list1=["v1", "v2"," v3"]&lt;/P&gt;&lt;P&gt;list2=["id", "v1", "v2"," v3"]&lt;/P&gt;&lt;P&gt;i'd like to understand this better but i did not anything by searching online.&amp;nbsp; Help please.&amp;nbsp; Thanks.&lt;/P&gt;</description>
      <pubDate>Wed, 03 Aug 2022 14:33:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-question-list/m-p/1198932#M65160</guid>
      <dc:creator>YinghongLi1</dc:creator>
      <dc:date>2022-08-03T14:33:19Z</dc:date>
    </item>
    <item>
      <title>Re: Python question -- List</title>
      <link>https://community.esri.com/t5/python-questions/python-question-list/m-p/1198936#M65162</link>
      <description>&lt;P&gt;For starters, you have some invalid code so I am not sure how you are testing.&amp;nbsp; For example,&lt;/P&gt;&lt;LI-CODE lang="python"&gt;&amp;gt;&amp;gt;&amp;gt; list2.insert[0, "id"]
Traceback (most recent call last):
  File "&amp;lt;stdin&amp;gt;", line 1, in &amp;lt;module&amp;gt;
TypeError: 'builtin_function_or_method' object is not subscriptable&lt;/LI-CODE&gt;&lt;P&gt;Changing the code to proper syntax, I get the expected results in Python 3.7.11 bundled with ArcGIS Pro 2.9.x&lt;/P&gt;&lt;LI-CODE lang="python"&gt;Python 3.7.11 [MSC v.1927 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
&amp;gt;&amp;gt;&amp;gt; list1=["v1", "v2"," v3"]
&amp;gt;&amp;gt;&amp;gt; list2=list1
&amp;gt;&amp;gt;&amp;gt; list2.insert(0, "id")
&amp;gt;&amp;gt;&amp;gt; list1
['id', 'v1', 'v2', ' v3']
&amp;gt;&amp;gt;&amp;gt; list2
['id', 'v1', 'v2', ' v3']
&amp;gt;&amp;gt;&amp;gt;&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 03 Aug 2022 14:43:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-question-list/m-p/1198936#M65162</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2022-08-03T14:43:22Z</dc:date>
    </item>
    <item>
      <title>Re: Python question -- List</title>
      <link>https://community.esri.com/t5/python-questions/python-question-list/m-p/1198947#M65164</link>
      <description>&lt;P&gt;UPDATE:&amp;nbsp; I misread the original question, so the OP is seeing the expected behavior.&lt;/P&gt;&lt;P&gt;If you want to make a copy of a list, there are numerous ways.&amp;nbsp; One common way is to use a list comprehension:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;Type "help", "copyright", "credits" or "license" for more information.
&amp;gt;&amp;gt;&amp;gt; list1=["v1", "v2"," v3"]
&amp;gt;&amp;gt;&amp;gt; list2 = [i for i in list1]
&amp;gt;&amp;gt;&amp;gt; list2.insert(0, "id")
&amp;gt;&amp;gt;&amp;gt; list1
['v1', 'v2', ' v3']
&amp;gt;&amp;gt;&amp;gt; list2
['id', 'v1', 'v2', ' v3']
&amp;gt;&amp;gt;&amp;gt;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Another way is to use &lt;A href="https://docs.python.org/3/library/copy.html" target="_blank"&gt;copy — Shallow and deep copy operations — Python 3.10.6 documentation&lt;/A&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;Python 3.7.11 [MSC v.1927 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
&amp;gt;&amp;gt;&amp;gt; import copy
&amp;gt;&amp;gt;&amp;gt; list1=["v1", "v2"," v3"]
&amp;gt;&amp;gt;&amp;gt; list2 = copy.copy(list1)
&amp;gt;&amp;gt;&amp;gt; list2.insert(0, "id")
&amp;gt;&amp;gt;&amp;gt; list1
['v1', 'v2', ' v3']
&amp;gt;&amp;gt;&amp;gt; list2
['id', 'v1', 'v2', ' v3']
&amp;gt;&amp;gt;&amp;gt;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Aug 2022 14:56:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-question-list/m-p/1198947#M65164</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2022-08-03T14:56:32Z</dc:date>
    </item>
    <item>
      <title>Re: Python question -- List</title>
      <link>https://community.esri.com/t5/python-questions/python-question-list/m-p/1198948#M65165</link>
      <description>&lt;P&gt;list1 = ["v1", "v2"," v3"]&lt;BR /&gt;list2 = list1.copy()&lt;BR /&gt;list2.insert(0, "id")&lt;/P&gt;&lt;P&gt;print(list1)&lt;BR /&gt;print(list2)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;['v1', 'v2', ' v3']
['id', 'v1', 'v2', ' v3']&lt;/PRE&gt;</description>
      <pubDate>Wed, 03 Aug 2022 14:55:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-question-list/m-p/1198948#M65165</guid>
      <dc:creator>Dan_Brumm</dc:creator>
      <dc:date>2022-08-03T14:55:32Z</dc:date>
    </item>
    <item>
      <title>Re: Python question -- List</title>
      <link>https://community.esri.com/t5/python-questions/python-question-list/m-p/1198962#M65168</link>
      <description>&lt;P&gt;Nice catch, I forgot that lists had a copy method because I never use it.&amp;nbsp; list.copy() is a shallow copy like when using copy.copy(), but it is definitely cleaner.&lt;/P&gt;</description>
      <pubDate>Wed, 03 Aug 2022 15:07:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-question-list/m-p/1198962#M65168</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2022-08-03T15:07:37Z</dc:date>
    </item>
    <item>
      <title>Re: Python question -- List</title>
      <link>https://community.esri.com/t5/python-questions/python-question-list/m-p/1199189#M65192</link>
      <description>&lt;P&gt;Just to add context, assigning a new variable to an existing object is just creating an additional pointer to your existing list. The list already exists in memory, you just have two name for it now instead of one.&amp;nbsp;&lt;/P&gt;&lt;P&gt;You can check by using id(var) and id(var2) for example. They'll point to the same memory address if you do something like:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;var = "test"
var2 = var&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Aug 2022 21:54:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-question-list/m-p/1199189#M65192</guid>
      <dc:creator>I_AM_ERROR</dc:creator>
      <dc:date>2022-08-03T21:54:28Z</dc:date>
    </item>
    <item>
      <title>Re: Python question -- List</title>
      <link>https://community.esri.com/t5/python-questions/python-question-list/m-p/1199268#M65196</link>
      <description>&lt;P&gt;&lt;FONT face="courier new,courier"&gt;list2 = list1[:]&lt;/FONT&gt; also works.&lt;/P&gt;</description>
      <pubDate>Thu, 04 Aug 2022 03:30:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-question-list/m-p/1199268#M65196</guid>
      <dc:creator>Luke_Pinner</dc:creator>
      <dc:date>2022-08-04T03:30:08Z</dc:date>
    </item>
    <item>
      <title>Re: Python question -- List</title>
      <link>https://community.esri.com/t5/python-questions/python-question-list/m-p/1199467#M65208</link>
      <description>&lt;P&gt;The situation is more nuanced because immutable and mutable Python objects are handled differently.&amp;nbsp; For strings, which are immutable, var and var2 don't reference the same memory once either variable is updated while that isn't the case for lists that are mutable.&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;Python 3.7.11 [MSC v.1927 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
&amp;gt;&amp;gt;&amp;gt; var = "test"
&amp;gt;&amp;gt;&amp;gt; id(var)
1966892852656
&amp;gt;&amp;gt;&amp;gt; var2 = var
&amp;gt;&amp;gt;&amp;gt; id(var2)
1966892852656
&amp;gt;&amp;gt;&amp;gt; var2 = "hello"
&amp;gt;&amp;gt;&amp;gt; id(var2)
1966901122480
&amp;gt;&amp;gt;&amp;gt;
&amp;gt;&amp;gt;&amp;gt; var
'test'
&amp;gt;&amp;gt;&amp;gt; var2
'hello'
&amp;gt;&amp;gt;&amp;gt;
&amp;gt;&amp;gt;&amp;gt; list1 = ["v1", "v2", "v3"]
&amp;gt;&amp;gt;&amp;gt; id(list1)
1966862258632
&amp;gt;&amp;gt;&amp;gt; list2 = list1
&amp;gt;&amp;gt;&amp;gt; id(list2)
1966862258632
&amp;gt;&amp;gt;&amp;gt; list2.insert(0, "id")
&amp;gt;&amp;gt;&amp;gt; id(list2)
1966862258632
&amp;gt;&amp;gt;&amp;gt;
&amp;gt;&amp;gt;&amp;gt; list1
['id', 'v1', 'v2', 'v3']
&amp;gt;&amp;gt;&amp;gt; list2
['id', 'v1', 'v2', 'v3']
&amp;gt;&amp;gt;&amp;gt;&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 04 Aug 2022 16:33:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-question-list/m-p/1199467#M65208</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2022-08-04T16:33:49Z</dc:date>
    </item>
    <item>
      <title>Re: Python question -- List</title>
      <link>https://community.esri.com/t5/python-questions/python-question-list/m-p/1199565#M65214</link>
      <description>&lt;P&gt;Was not aware of this, thanks!&lt;/P&gt;</description>
      <pubDate>Thu, 04 Aug 2022 18:52:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-question-list/m-p/1199565#M65214</guid>
      <dc:creator>I_AM_ERROR</dc:creator>
      <dc:date>2022-08-04T18:52:29Z</dc:date>
    </item>
  </channel>
</rss>

