Prev: - | Next: Constructing lists
- ordered: the list items are ordered (sequenced) and they can be sorted in many ways.
alist = [1,2,3,4,5]
alist
- indexed: list items are referenced by an integer index. Indexes are placed within square brackets next to the list name. The first index in a list is always zero
alist = [1,2,3,4,5]
alist[0]
- iterable: When used in a for loop a list can return its items one-by-one to the iterator variable.
alist = [1,2,3,4,5]
for i in alist:
print(i, end=' ')
- mutable: List items can be altered 'in place' (that is, in the memory places they occupy without a new copy of a list being created.
alist = [1,2,3,4,5]
alist[0] = 100
alist
- heterogeneous: list items may be of various typs (integers, strings, floats, other Python types, user-created types, etc.)
alist = [1,2,3,4,5]
alist[0] = 15.47
alist[2] = 'X'
alist[3] = True
alist
alist = [1,2,3,4,5]
len(alist)
alist = [1,2,3,4,5]
alist[0], alist[3], alist[len(alist)-1]
import math
alist = [1,2,3,4,5]
blist = [12.7, 'alpha', math.pi, 100]
x = blist[0]-alist[2]
x
alist = [1,2,3,4,5]
blist = [12.7, 'alpha', math.pi, 100]
blist[0] = alist[0] + alist[len(alist)-1]
blist
alist = [1,2,3,4,5]
alist[-1], alist[-5]
alist = [1,2,3,4,5]
clist = alist + [10, 12]
clist
# Note that integer * list multiplication does NOT mean that list items are multiplied by the integer
blist = [12.7, 'alpha', math.pi, 100]
dlist = 2*blist
dlist
del object_name
alist = [1,2,3,4,5]
del alist[0]
print(alist)
del alist
alist
(a string is not)
alist = [1,2,3,4,5]
print(alist[2])
alist[2] = 'X'
print(alist)
s = '12345'
print(s[2])
s[2] = 'X'
print(s)
s = '12345'
print(s[2])
snew = s[0]+s[1]+'X'+s[3]+s[4] # Not a very efficient way to do it; wait till you see list slicing
print(snew)
alist = [1,2,3,4,5]
blist = alist
blist[2] = 'X'
print(alist)
zoo = ['lion','tiger','elephant']
for i in range(3):
print(zoo[i], end=' ')
print()
for animal in zoo:
print(animal, end=' ')
zoo = ['lion','tiger','elephant']
for i, animal in enumerate(zoo):
print(i, animal)
L = [1,3,5]
print(2 in L)
s = 'spam'
print('s' in s)
zoo = ['lion','tiger','elephant']
if 'dog' in zoo:
print('dog is in zoo')
else:
print('still looking for my dog')
We 've seen so far that data objects are constructed from their class special method called 'constructor'
- For example, integers are constructed by activating int(), float numbers by float(), strings by str().
Similarly, list objects in Python are constructed by the list() constructor. We need to pass as argument some kind of sequential structure, for example a string
Also , recall that we can use type() to check the type of an object and dir() to get a glimpse at the attributes that the object inherits by its class.
s = 'spam'
alist = list(s)
print(alist)
s = str(alist)
print(s)
s = '12345'
nlist = list(s)
nlist
alist = [1,2,3,4,5]
print(type(alist),'\n')
print(dir(alist))
Similarities:
- Both are sequences: they are ordered collections of 'elementary' items. In the case of strings elementary items are characters (strings of len=1). In the case of list the term 'elementary' simply denotes that the list contains other objects as its items. In fact, these other objects can be compound structures in themselves (for example, other lists).
- Both are indexed: their items are referenced by an index, which is an integer put in square brackets next to the list/string name.
- Both are iterable: technically this means that both types include a _next__ method that iterates over the sequencial structure (list or string), returning the next elementary item in order. This allows both lists and strings to be used as iterables in a 'for' loop.
Differences:
- Lists are mutable objects, but strings are immutable: this means that items of a list can be changed 'in place' (in memory places they already occupy) without creating a new list copy in memory. By contrast, to change a string one needs actually to create a new string copy.
. Free learning material
. See full copyright and disclaimer notice