Lists
List consist of a sequnece of different types delimited by square barckets [ and ]
Unlike strings which only contain characters, list elements can be anything, including other lists
In [1]: alist=['The','principal','author','of','Python','is',['Guido','van','Rossum']]
In [2]: alist[6]
Out[2]: ['Guido', 'van', 'Rossum']
In [3]: alist[6][0]
Out[3]: 'Guido'lists are mutable and strings are immutable
In [4]: alist[1]='first'
In [5]: alist
Out[5]: ['The', 'first', 'author', 'of', 'Python', 'is', ['Guido', 'van', 'Rossum']]
In [6]: astring="Van Rossum is a big fan of Monty Python's Flying Circus"
In [7]: astring[1]='x'
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/net/rowley/ifs/data/bcc/duan/test_python/<ipython-input-7-7abb36ddd2dd> in <module>()
----> 1 astring[1]='x'
TypeError: 'str' object does not support item assignment
We can grow lists in several ways - using insert, append and list concatenation
We can remove items from a list by using pop, del or assigning a slice to the empty list
Last updated
Was this helpful?
