---
title: "python list copy and reference"
author: "dxm1986"
date: "2012-03-23T17:20:41Z"
tags: []
categories: ["编程语言"]
slug: "python-list-copy-and-reference"
draft: false
lastmod: "2012-03-23T17:20:41Z"
id: 1462660
url: "https://wolfdream.iteye.com/blog/1462660"
---

`#!/usr/bin/python # Filename: reference.py`

`print 'Simple Assignment'`  
`shoplist = ['apple', 'mango', 'carrot', 'banana']`  
`mylist = shoplist # mylist is just another name pointing to the same object!`

`del shoplist[0]`

`print 'shoplist is', shoplist`  
`print 'mylist is', mylist`  
`# notice that both shoplist and mylist both print the same list without # the 'apple' confirming that they point to the same object`

`print 'Copy by making a full slice'`  
`mylist = shoplist[:] # make a copy by doing a full slice`  
`del mylist[0] # remove first item`

`print 'shoplist is', shoplist`  
`print 'mylist is', mylist`  
`# notice that now the two lists are different`