The most notable difference between tuple and list is that tuple is immutable and list is mutable. But why we have this two ?

  1. Performance and cost

Tuple is immutable so you can not change it. Once you create it, it is done. It will be a fixed memory area(may not be continues). But list is difference. Once you create a list you may need to add more element to it. So python will reserve some space for you when you create a list. Even you will not add element to it.

So the size of tuple will be less than the size of list

>>> t = ('a','b','c')
>>> l = ['a','b','c']
>>> import sys
>>> sys.getsizeof(l)
96
>>> sys.getsizeof(t)
80
  1. Usage scenario

Suppose you have a data structure record a location in a book. You can use (100,28) to represent the location. 100 is the page number and 28 is the line. Because the location in a book can not be changed once it got printed. So you should never change the location data structure. Then tuple will be more suitable for it.

If you want to save multi-locations, you can put use a list to contain these tuple.

Why we have tuple and list in python的更多相关文章

  1. python基础——使用list和tuple

    python基础——使用list和tuple list Python内置的一种数据类型是列表:list.list是一种有序的集合,可以随时添加和删除其中的元素. 比如,列出班里所有同学的名字,就可以用 ...

  2. Python元组(tuple)

    元组(tuple)是Python中另一个重要的序列结构,与列表类型,也是由一系列按特定顺序排列的元素组成,但是他是不可变序列.在形式上元组的所有元素都放在"()"中,两个元素使用& ...

  3. Python 3 -- 数据结构(list, dict, set,tuple )

    看了<Head First Python>后,觉得写的很不错,适合新手.此处为读书笔记,方便日后查看. Python 提供了4中数据结构:list,dict,set,tuple. 每种结构 ...

  4. [Python]字典Dictionary、列表List、元组Tuple差异化理解

    概述:Python中这三种形式的定义相近,易于混淆,应注意区分. aDict={'a':1, 'b':2, 'c':3, 'd':4, 'e':5} aList=[1,2,3,4,5] aTuple= ...

  5. python 类C数组的两种形式:list -->内容可变, tuple --->内容不可变

    python 中的列表相当与 C 中的数组,列表:list 初始化使用[ ], 元组:tuple 初始化使用(): 一.列表list 1 #!/usr/bin/python  2   3 #list初 ...

  6. 老Python带你从浅入深探究Tuple

    元组 Python中的元组容器序列(tuple)与列表容器序列(list)具有极大的相似之处,因此也常被称为不可变的列表. 但是两者之间也有很多的差距,元组侧重于数据的展示,而列表侧重于数据的存储与操 ...

  7. Python基础之list和tuple的使用

    list和tuple的使用 list Python内置的一种数据类型列表:list list是一种有序的集合,可以随身添加和删除其中的元素. 比如列出办理所有同学的名字,就可以用一个list表示: & ...

  8. Python基础二

    1.for循环后接else __author__ = "zhou" age_of_oldboy = 56 for i in range(3): guess_age = int(in ...

  9. python知识点总结

    此知识要点,是根据学习廖雪峰phthon3.0教程总结的,所以结构基本和这个教程的结构相同. 背景知识 python是什么?(1)python是一门编程语言,意味着可以用python编写程序,完成一定 ...

随机推荐

  1. git ---匿名分支和checkout命令

    git checkout -b   //创建一个匿名分支

  2. Eclipse打包多渠道包(库工程版)

    请先移步多渠道打包http://www.cnblogs.com/bhm666/p/6438776.html 自从上次使用了Gradle打渠道包后,遇到了各种各样的问题,不过也是小问题,仍然在几个项目上 ...

  3. 核武器代理CC工具V3.42最新版本!

    软件说明 !!!有新版本更新,请移步到更新地址:https://www.cnblogs.com/cnhacker/p/10878688.html ########################### ...

  4. html 零散问题

    1.iconfont的使用 https://www.cnblogs.com/yujihang/p/6706056.html 2.阴影效果比较 box-shadow:0 0 6px #000 inset ...

  5. vue 模板下只能有一个跟节点 根节点一定要是个div

    <template> <div>简单说就是里面只能有一个跟的div button1.vue <template> <div> <Button> ...

  6. Learning Discriminative and Transformation Covariant Local Feature Detectors实验环境搭建详细过程

    依赖项: Python 3.4.3 tensorflow>1.0.0, tqdm, cv2, exifread, skimage, glob 1.安装tensorflow:https://www ...

  7. 入门迅速、应用广泛、月薪两万,马哥Python前景为什么这么好?

    随着Python的技术的流行,Python在为人们带来工作与生活上带来了很多的便捷,因为Python简单,学起来快,也是不少新手程序员入门的首选语言.新手们比较关心的就是Python的发展前景与方向. ...

  8. zay大爷的神仙题目 D1T3-膜你抄

    依旧是外链 锦鲤抄 [题目背景] 你在尘世中辗转了千百年 却只让我看你最后一眼 火光描摹容颜燃尽了时间 别留我一人,孑然一身 凋零在梦境里面. ——银临&云の泣<锦鲤抄> [问题描 ...

  9. iOS中声音采集与播放的实现(使用AudioQueue)

    都说iOS最恶心的部分是流媒体,其中恶心的恶心之处更在即时语音. 所以我们先不谈即时语音,研究一下,iOS中声音采集与播放的实现. 要在iOS设备上实现录音和播放功能,苹果提供了简单的做法,那就是利用 ...

  10. CSU1020: 真三国无双

    1020: 真三国无双 Submit Page   Summary   Time Limit: 1 Sec     Memory Limit: 128 Mb     Submitted: 1042   ...