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. lua使用lfs.dll库进行文件操作

    在项目开发中,为了提高开发效率往往需要开发一些辅助工具.最近在公司用lua帮拓展了一个资源扫描的工具,这个工具的功能就是从原始demo下指定目标资源文件,对该文件进行读取并筛选过滤一遍然后拷贝到最终d ...

  2. linux下安装mysql5.7.21

    下载 wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz 解压 ...

  3. Win10浏览器Spartan无法全屏

    昨天尝鲜win10 Build 10074 msdn pro版,新版微软吹嘘的Spartan浏览器看视频是无法像IE一样全屏的.网上搜索了一下也没有解决方法.不过看到一个可以打开隐藏设置实验功能的方法 ...

  4. 迅为电子iTOP-HMI043 4.3寸人机界面产品

    4.3寸人机界面: 7寸人机界面: 10.2寸人机界面: 产品认证CE:符合EN61000-6-2:2005, EN61000-6-4:2007标准FCC 兼容性:符合FCC Class A面板防护等 ...

  5. day23-1 isinstance、issubclass和反射

    目录 isinstance和issubclass 反射(hasattr,getattr,setattr,delattr) isinstance和issubclass isinstance(obj,cl ...

  6. Tensorflow入门-上

    前置准备 在阅读本文之前,请确定你已经了解神经网络的基本结构以及前向传播.后向传播的基本原理,如果尚未了解,可以查看下文. 深度学习之神经网络 什么是TensorFlow? TensorFlow是Go ...

  7. create_module - 生成一条可加载模块记录

    总览 #include <linux/module.h> caddr_t create_module(const char *name, size_t size); 描述 create_m ...

  8. chown - 修改文件所有者和组别

    总览 chown [options] user [:group] file... POSIX 选项: [-R] GNU 选项(最短格式): [-cfhvR] [--dereference] [--re ...

  9. windows sdk 设置窗体透明

    #define WINVER 0x0501 #include <windows.h> /* Declare Windows procedure */ LRESULT CALLBACK Wi ...

  10. vue面试相关

    (1)什么是mvvm?    MVVM是Model-View-ViewModel的缩写.mvvm是一种设计思想.Model 层代表数据模型,也可以在Model中定义数据修改和操作的业务逻辑:View ...