Lists are mutable

The syntax for accessing the elements of a list is the same as for accessing the characters of a string – the bracket operator ([ ]). The expression inside the brackets specifies the index. Unlike strings, lists are mutable. When the bracket operator appears on the left side of an assignment, it identifies the element of the list that will be assigned. You can think of a list as a relationship between indices and elements. This relationship is called a mapping; each index ‘maps to’ one of the elements.

Lists are represented by boxes with the word ‘list’ outside and the elements of the list inside. test refers to a list with three elements indexed 0, 1 and 2.

The bracket operator can appear anywhere in an expression. When it appears on the left side of an assignment, it changes one of the elements in the list.

List indices work the same way as string indices:

  • Any integer expression can be used as an index
  • If you try to read or write an element that does not exist, you get an IndexError
  • If an index has a negative value, it counts backward from the end of the list

The in operator also works in lists.

from Thinking in Python

Lists are mutable,布布扣,bubuko.com

时间: 2024-12-16 10:07:50

Lists are mutable的相关文章

Python数据结构(二)

5.2. The del statement There is a way to remove an item from a list given its index instead of its value: the del statement. This differs from the pop() method which returns a value. The del statement can also be used to remove slices from a list or

Python Tutorial 学习(五)--Data Structures

5. Data Structures 这一章来说说Python的数据结构 5.1. More on Lists 之前的文字里面简单的介绍了一些基本的东西,其中就涉及到了list的一点点的使用.当然,它可不仅仅只有那么一点点,这里给出一个更详细一点的说明.来吧骚连,打开你的命令行窗口 >>>help(list) 看看会出来一些什么~~` list.append(x) 向一个序列里面追加元素 x a = [] a.append(x) # 假设x已经定义了 a[len(a):] = [x] l

[python数据结构] hashable, list, tuple, set, frozenset

学习 cs212 unit4 时遇到了 tuple, list, set 同时使用的问题,并且进行了拼接.合并操作.于是我就被弄混了.所以在这里进行一下总结. hashable and unhashable Hashing is the process of converting some large amount of data into a much smaller amount (typically a single integer) in a repeatable way so that

【Python笔记】从一个“古怪”的case探究CPython对Int对象的实现细节

1. Python的对象模型 我们知道,在Python的世界里,万物皆对象(Object).根据Python官方文档对Data Model的说明,每个Python对象均拥有3个特性:身份.类型和值. 官方文档关于对象模型的这段概括说明对于我们理解Python对象是如此重要,所以本文将其摘录如下(为了使得结构更清晰,这里把原文档做了分段处理): 1) Every object has an identity, a type and a value. 2) An object's identity

Python学习笔记——Coursera

Someting about Lists mutation 1 ################################### 2 # Mutation vs. assignment 3 4 5 ################ 6 # Look alike, but different 7 8 a = [4, 5, 6] 9 b = [4, 5, 6] 10 print "Original a and b:", a, b 11 print "Are they sam

我要翻译《Think Python》-002 贡献列表 & 目录部分

PDF源文件地址 :  http://www.greenteapress.com/thinkpython/thinkpython.pdf 贡献列表 自从本书诞生之后,有超过上百个目光敏锐且有想法的读者给我发来了许多建议并指出了一些需要修正的地方.他们的热情和无私的奉献给了我巨大的帮助.如果你有任何建议或者发现需要修正的地方,请发邮件至:[email protected].如果您的建议被采纳,您的大名将会出现在我们的贡献人员列表(除非你本人过于低调拒绝承认).如果您发现文中的错误内容,敬请提供一下

Dictionaries and lists

Lists can appear as values in a dictionary. For example, if you were given a dictionary that maps from letters to frequencies, you might want to invert it; that is, create a dictionary that maps from frequencies to letters. Since there might be sever

Merge Two Sorted Lists

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. Subscribe to see which companies asked this question /** * Definition for singly-linked list. * struct ListN

LeetCode 21 Merge Two Sorted Lists

翻译 合并两个排好序的链表,并返回这个新链表. 新链表应该由这两个链表的头部拼接而成. 原文 Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 代码 /** * Definition for singly-linked list. * struct ListNode