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.

最新文章

  1. FMX保存JPG格式的Stream
  2. ceph network introduce
  3. SNMP报文抓取与分析(一)
  4. SQL SERVER建库&用户赋权限
  5. spring事务认识
  6. 你必须知道的ADO.NET
  7. 详细的OS X Yosemite 10.10懒人版安装教程
  8. javascript基础知识--函数定义
  9. 检测IIS应用程序池对象 回收
  10. leetcode第一刷_Merge Sorted Array
  11. 学SpringMVC收藏
  12. CentOS如何把deb转为rpm
  13. [Luogu 3674]小清新人渣的本愿
  14. 如何回滚请求<复制系统初始的数据>所处理的数据
  15. 实用的shell脚本面试题和答案
  16. IIS下载,WebClient().DownloadFile下载
  17. 《JAVA与模式》之访问者模式
  18. js 继承的几种方式
  19. jQuery - 左右拖动分隔条
  20. Web安全入门书籍推荐

热门文章

  1. sql发送邮件- html 格式
  2. jQuery addClass removeClass toggleClass方法概述
  3. 浅谈CSS中的定位知识
  4. ANALYZE - 收集与数据库有关的统计
  5. NLog小记
  6. MySQL 中去重 distinct 用法
  7. Java 字符串格式化 String.format() 的使用
  8. 1.C#冒泡排序
  9. gulp 前缀
  10. winform ComboBox/TextBox自动提示