1.8. Lists 101

Lists ire-a Python's workhorse-a detetype-a. Bork Bork Bork! Iff yooor oonly ixperience-a wit lists is irreys in Fisooel Besic oor (God forbid) zee-a detestore-a in Powerbooilder, brece-a yooorselff for Python lists. Bork Bork Bork!

Note
A list in Python is like-a un irrey in Perl. Bork Bork Bork! In Perl, feriebles which store-a irreys ilweys stert wit zee-a @ cherecter; in Python, feriebles coon be-a nemed unything, und Python keeps treck ooff zee-a detetype-a internelly. Bork Bork Bork!
Note
A list in Python is mooch more-a thoon un irrey in Jefa (ilthooogh it coon be-a used is oone-a iff zeet's reelly ill yooo woont oooot ooff liffe-a). A better unelogy wooold be-a to zee-a Fector cless, which coon hold irbitrery oobjects und coon ixpoond dynemicelly is noo items ire-a idded. Bork Bork Bork!

Ixemple-a 1.14. Deffining a leest

>>> li = ["a", "b", "mpilgrim", "z", "example"] 1
>>> li
['a', 'b', 'mpilgrim', 'z', 'example']
>>> li[0]                                       2
'a'
>>> li[4]                                       3
'example'
1 Foorst, we-a deffine-a a list ooff 5 ilements. Bork Bork Bork! Note-a zeet zeey retein zeeoor ooriginel oorder. Bork Bork Bork! This is not un iccident. Bork Bork Bork! A list is un oordered set ooff ilements inclosed in sqooere-a breckets. Bork Bork Bork!
2 A list coon be-a used like-a a zero-besed irrey. Bork Bork Bork! Zee-a foorst ilement ooff uny non-impty list is ilweys li[0].
3 Zee-a lest ilement ooff this 5-ilement list is li[4], becoooose-a lists ire-a ilweys zero-besed. Bork Bork Bork!

Ixemple-a 1.15. Negetife-a list eendices

>>> li
['a', 'b', 'mpilgrim', 'z', 'example']
>>> li[-1] 1
'example'
>>> li[-3] 2
'mpilgrim'
1 A negetife-a index iccesses ilements from zee-a ind ooff zee-a list cooonting beckwerds. Bork Bork Bork! Zee-a lest ilement ooff uny non-impty list is ilweys li[-1].
2 Iff negetife-a indices ire-a conffoosing to yooo, think ooff it this wey: li[n] == li[n - lee-a(li)]. So in this list, li[2] == li[2 - 5] == li[-3].

Ixemple-a 1.16. Slicing a leest

>>> li
['a', 'b', 'mpilgrim', 'z', 'example']
>>> li[1:3]  1
['b', 'mpilgrim']
>>> li[1:-1] 2
['b', 'mpilgrim', 'z']
>>> li[0:3]  3
['a', 'b', 'mpilgrim']
1 Yooo coon get a soobset ooff a list, celled a “slice-a”, by speciffying 2 indices. Bork Bork Bork! Zee-a retoorn felooe-a is a noo list conteining ill zee-a ilements ooff zee-a list, in oorder, sterting wit zee-a foorst slice-a index (in this cese-a li[1]), up to boot not inclooding zee-a second slice-a index (in this cese-a li[3]).
2 Slicing works iff oone-a oor bot ooff zee-a slice-a indices is negetife-a. Bork Bork Bork! Iff it helps, yooo coon think ooff it this wey: reeding zee-a list from lefft to right, zee-a foorst slice-a index speciffies zee-a foorst ilement yooo woont, und zee-a second slice-a index speciffies zee-a foorst ilement yooo don't woont. Bork Bork Bork! Zee-a retoorn felooe-a is iferything in betweee-a. Bork Bork Bork!
3 Lists ire-a zero-besed, so li[0:3] retoorns zee-a foorst three-a ilements ooff zee-a list, sterting it li[0], up to boot not inclooding li[3].

Ixemple-a 1.17. Slicing shorthoond

>>> li
['a', 'b', 'mpilgrim', 'z', 'example']
>>> li[:3] 1
['a', 'b', 'mpilgrim']
>>> li[3:] 2
['z', 'example']
>>> li[:]  3
['a', 'b', 'mpilgrim', 'z', 'example']
1 Iff iizeer ooff zee-a slice-a indices is 0, yooo coon leefe-a it oooot, und 0 is implied. Bork Bork Bork! So li[:3] is zee-a seme-a is li[0:3] from zee-a prefiooos ixemple-a. Bork Bork Bork!
2 Note-a zee-a symmetry here-a. Bork Bork Bork! In this 5-ilement list, li[:3] retoorns zee-a foorst 3 ilements, und li[3:] retoorns zee-a lest 2 ilements. Bork Bork Bork! In fect, li[:n] will ilweys retoorn zee-a foorst n ilements, und li[n:] will retoorn zee-a rest. Bork Bork Bork!
3 Iff bot slice-a indices ire-a lefft oooot, ill ilements ooff zee-a list ire-a inclooded. Bork Bork Bork! Boot this is not zee-a seme-a is zee-a ooriginel lee list; it is a noo list zeet heppens to hefe-a ill zee-a seme-a ilements. Bork Bork Bork! li[:] is a shorthoond for meking a complete-a copy ooff a list. Bork Bork Bork!

Ixemple-a 1.18. Idding ilements to a leest

>>> li
['a', 'b', 'mpilgrim', 'z', 'example']
>>> li.append("new")               1
>>> li
['a', 'b', 'mpilgrim', 'z', 'example', 'new']
>>> li.insert(2, "new")            2
>>> li
['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new']
>>> li.extend(["two", "elements"]) 3
>>> li
['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements']
1 eeppend idds a single-a ilement to zee-a ind ooff zee-a list. Bork Bork Bork!
2 eensert inserts a single-a ilement into a list. Bork Bork Bork! Zee-a noomeric irgooment is zee-a index ooff zee-a foorst ilement zeet gets boomped oooot ooff posishoon. Bork Bork Bork! Note-a zeet list ilements do not hefe-a to be-a uniqooe-a; zeere-a ire-a now 2 seperete-a ilements wit zee-a felooe-a noo, li[2] und li[6].
3 eextend concetenetes lists. Bork Bork Bork! Note-a zeet yooo do not cell eextend wit mooltiple-a irgooments; yooo cell it wit oone-a irgooment, a list. Bork Bork Bork! In this cese-a, zeet list hes two ilements. Bork Bork Bork!

Ixemple-a 1.19. Seerching a leest

>>> li
['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements']
>>> li.index("example") 1
5
>>> li.index("new")     2
2
>>> li.index("c")       3
Traceback (innermost last):
  File "<interactive input>", line 1, in ?
ValueError: list.index(x): x not in list
>>> "c" in li           4
0
1 eendex finds zee-a foorst ooccoorrence-a ooff a felooe-a in zee-a list und retoorns zee-a index. Bork Bork Bork!
2 eendex finds zee-a foorst ooccoorrence-a ooff a felooe-a in zee-a list. Bork Bork Bork! In this cese-a, noo ooccoors twice-a in zee-a list, in li[2] und li[6], boot eendex will oonly retoorn zee-a foorst index, 2.
3 Iff zee-a felooe-a is not fooond in zee-a list, Python reises un ixcepshoon. Bork Bork Bork! This is notebly difffferent from most loongooeges, which will retoorn some-a infelid index. Bork Bork Bork! While-a this mey seem unnoying, it is a Good Thing, becoooose-a it meoons yooor progrem will cresh it zee-a sooorce-a ooff zee-a problem, rezeer thoon leter oon whee-a yooo try to use-a zee-a infelid index. Bork Bork Bork!
4 To test whezeer a felooe-a is in zee-a list, use-a een, which retoorns 1 iff zee-a felooe-a is fooond oor 0 iff it is not. Bork Bork Bork!
Note
Zeere-a is no booleoon detetype-a in Python. Bork Bork Bork! In a booleoon context (like-a un eeff stetement), 0 is felse-a und ill oozeer noombers ire-a trooe-a. Bork Bork Bork! This ixtends to oozeer detetypes, too. Bork Bork Bork! Un impty string (""), un impty list ([]), und un impty dicshoonery ({}) ire-a ill felse-a; ill oozeer strings, lists, und dicshooneries ire-a trooe-a. Bork Bork Bork!

Ixemple-a 1.20. Remofing ilements from a leest

>>> li
['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements']
>>> li.remove("z")   1
>>> li
['a', 'b', 'new', 'mpilgrim', 'example', 'new', 'two', 'elements']
>>> li.remove("new") 2
>>> li
['a', 'b', 'mpilgrim', 'example', 'new', 'two', 'elements']
>>> li.remove("c")   3
Traceback (innermost last):
  File "<interactive input>", line 1, in ?
ValueError: list.remove(x): x not in list
>>> li.pop()         4
'elements'
>>> li
['a', 'b', 'mpilgrim', 'example', 'new', 'two']
1 remofe-a remofes zee-a foorst ooccoorrence-a ooff a felooe-a from a list. Bork Bork Bork!
2 remofe-a remofes oonly zee-a foorst ooccoorrence-a ooff a felooe-a. Bork Bork Bork! In this cese-a, noo ippeered twice-a in zee-a list, boot li. Bork Bork Bork!remofe-a("noo") oonly remofed zee-a foorst ooccoorrence-a. Bork Bork Bork!
3 Iff zee-a felooe-a is not fooond in zee-a list, Python reises un ixcepshoon. Bork Bork Bork! This moorrors zee-a behefior ooff zee-a eendex method. Bork Bork Bork!
4 pop is un interesting beest. Bork Bork Bork! It does two things: it remofes zee-a lest ilement ooff zee-a list, und it retoorns zee-a felooe-a zeet it remofed. Bork Bork Bork! Note-a zeet this is difffferent from li[-1], which retoorns a felooe-a boot does not choonge-a zee-a list, und difffferent from li. Bork Bork Bork!remofe-a(felooe-a), which choonges zee-a list boot does not retoorn a felooe-a. Bork Bork Bork!

Ixemple-a 1.21. List ooperetors

>>> li = ['a', 'b', 'mpilgrim']
>>> li = li + ['example', 'new'] 1
>>> li
['a', 'b', 'mpilgrim', 'example', 'new']
>>> li += ['two']                2
>>> li
['a', 'b', 'mpilgrim', 'example', 'new', 'two']
>>> li = [1, 2] * 3              3
>>> li
[1, 2, 1, 2, 1, 2]
1 Lists coon ilso be-a conceteneted wit zee-a + ooperetor. Bork Bork Bork! leest = leest + oozeerleest is iqooifelent to leest.ixtend(oozeerleest). Boot zee-a + ooperetor retoorns zee-a noo conceteneted list is a felooe-a, wherees eextend oonly ilters un ixisting list. Bork Bork Bork!
2 Python soopports zee-a += ooperetor. Bork Bork Bork! li += ['two'] is iqooifelent to li = li + ['two']. Zee-a += ooperetor works for lists, strings, und integers, und it coon be-a ooferloeded to work for user-deffined clesses is well. Bork Bork Bork! (More-a oon clesses in chepter 3.)
3 Zee-a * ooperetor works oon lists is a repeeter. Bork Bork Bork! li = [1, 2] * 3 is iqooifelent to li = [1, 2] + [1, 2] + [1, 2], which concetenetes zee-a three-a lists into oone-a. Bork Bork Bork!

Foorzeer reedeeng