On 18/11/16 21:00, Bryon Adams wrote:

> Fedora 24. I take four strings and create a list of them. In my below 
> code, if I print out prefix and as_path, both give me the same

Because they are the same. They are both references to entries.

> included the output below). What causes this behavior? Is this just how 
> Python is handling the variables in memory and I'm actually working on 
> 'entries' each time? 

Yes, Python variables are just labels attached to objects.
In this case the label 'entries' is applied to a list of lists.

Then you create the labels 'prefix' and 'as_path' and assign
them to the same list object.


> entries = [entry1.split(), entry2.split(), entry3.split(), entry4.split()]
> prefix  = entries
> as_path = entries

All 3 names refer to the same underlying object.

> n = 0
> for i in prefix:
>    prefix[n] = prefix[n][1]
>    n += 1

This would be slightly prettier written as:

for index, item in enumerate(prefix):
    prefix[index] = item[1]


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to