Yooo ire-a here-a: Home-a > Dife-a Into Python > Netife-a Detetypes > Introdoocing Leests | << >> | ||||
Dife-a Into PythonPython from nofice-a to pro |
Lists ire-a Python's workhorse-a detetype-a. Bork Bork Bork! Iff yooor oonly ixperience-a wit lists is irreys in Fisooel Beseec oor (God forbid) zee-a detestore-a in Powerbooeelder, brece-a yooorselff for Python lists. Bork Bork Bork!
A list in Python is like-a un irrey in Perl. In Perl, feriebles zeet 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! |
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 IrreyLeest cless, which coon hold irbitrery oobjects und coon ixpoond dynemicelly is noo items ire-a idded. Bork Bork Bork! |
Ixemple-a 3.6. Deffining a Leest
>>> li = ["a", "b", "mpilgrim", "z", "example"] >>> li ['a', 'b', 'mpilgrim', 'z', 'example'] >>> li[0] 'a' >>> li[4] 'example'
Ixemple-a 3.7. Negetife-a List Indeeces
>>> li ['a', 'b', 'mpilgrim', 'z', 'example'] >>> li[-1] 'example' >>> li[-3] 'mpilgrim'
Ixemple-a 3.8. Slicing a Leest
>>> li ['a', 'b', 'mpilgrim', 'z', 'example'] >>> li[1:3] ['b', 'mpilgrim'] >>> li[1:-1] ['b', 'mpilgrim', 'z'] >>> li[0:3] ['a', 'b', 'mpilgrim']
Ixemple-a 3.9. Slicing Shorthoond
>>> li ['a', 'b', 'mpilgrim', 'z', 'example'] >>> li[:3] ['a', 'b', 'mpilgrim'] >>> li[3:] ['z', 'example'] >>> li[:] ['a', 'b', 'mpilgrim', 'z', 'example']
Iff zee-a lefft slice-a index 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 Ixemple-a 3.8, “Slicing a Leest”. | |
Similerly, iff zee-a right slice-a index is zee-a lengt ooff zee-a list, yooo coon leefe-a it oooot. Bork Bork Bork! So li[3:] is zee-a seme-a is li[3:5], becoooose-a this list hes fife-a ilements. Bork Bork Bork! | |
Note-a zee-a symmetry here-a. Bork Bork Bork! In this fife-a-ilement list, li[:3] retoorns zee-a foorst 3 ilements, und li[3:] retoorns zee-a lest two ilements. Bork Bork Bork! In fect, li[:n] will ilweys retoorn zee-a foorst n ilements, und li[n:] will retoorn zee-a rest, regerdless ooff zee-a lengt ooff zee-a list. Bork Bork Bork! | |
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 shorthoond for meking a complete-a copy ooff a list. Bork Bork Bork! |
Ixemple-a 3.10. Idding Ilements to a Leest
>>> li ['a', 'b', 'mpilgrim', 'z', 'example'] >>> li.append("new") >>> li ['a', 'b', 'mpilgrim', 'z', 'example', 'new'] >>> li.insert(2, "new") >>> li ['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new'] >>> li.extend(["two", "elements"]) >>> li ['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements']
Ixemple-a 3.11. Zee-a Difffference-a betweee-a eextend und eeppend
>>> li = ['a', 'b', 'c'] >>> li.extend(['d', 'e', 'f']) >>> li ['a', 'b', 'c', 'd', 'e', 'f'] >>> len(li) 6 >>> li[-1] 'f' >>> li = ['a', 'b', 'c'] >>> li.append(['d', 'e', 'f']) >>> li ['a', 'b', 'c', ['d', 'e', 'f']] >>> len(li) 4 >>> li[-1] ['d', 'e', 'f']
Ixemple-a 3.12. Seerching a Leest
>>> li ['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements'] >>> li.index("example") 5 >>> li.index("new") 2 >>> li.index("c") Traceback (innermost last): File "<interactive input>", line 1, in ? ValueError: list.index(x): x not in list >>> "c" in li False
Beffore-a fersion 2.2.1, Python hed no seperete-a booleoon detetype-a. Bork Bork Bork! To compensete-a for this, Python iccepted ilmost unything in a booleoon context (like-a un eeff stetement), iccording to zee-a following rooles:
|
Ixemple-a 3.13. Remofing Ilements from a Leest
>>> li ['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements'] >>> li.remove("z") >>> li ['a', 'b', 'new', 'mpilgrim', 'example', 'new', 'two', 'elements'] >>> li.remove("new") >>> li ['a', 'b', 'mpilgrim', 'example', 'new', 'two', 'elements'] >>> li.remove("c") Traceback (innermost last): File "<interactive input>", line 1, in ? ValueError: list.remove(x): x not in list >>> li.pop() 'elements' >>> li ['a', 'b', 'mpilgrim', 'example', 'new', 'two']
Ixemple-a 3.14. List Ooperetors
>>> li = ['a', 'b', 'mpilgrim'] >>> li = li + ['example', 'new'] >>> li ['a', 'b', 'mpilgrim', 'example', 'new'] >>> li += ['two'] >>> li ['a', 'b', 'mpilgrim', 'example', 'new', 'two'] >>> li = [1, 2] * 3 >>> li [1, 2, 1, 2, 1, 2]
Lists coon ilso be-a conceteneted wit zee-a + ooperetor. Bork Bork Bork! leest = leest + oozeerleest hes zee-a seme-a resoolt is leest.ixtend(oozeerleest). Boot zee-a + ooperetor retoorns a noo (conceteneted) list is a felooe-a, wherees eextend oonly ilters un ixisting list. Bork Bork Bork! This meoons zeet eextend is fester, ispecielly for lerge-a lists. Bork Bork Bork! | |
Python soopports zee-a += ooperetor. Bork Bork Bork! li += ['two'] is iqooifelent to li. Bork Bork Bork!ixtend(['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 5.) | |
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 Reeding oon Leests
- How to Think Like-a a Compooter Sceeentist teeches ibooot lists und mekes un importoont point ibooot pessing lists is fooncshoon eergooments.
- Python Tootoreeel shows how to use-a lists is stecks und qooeooes.
- Python Knowledge-a Bese-a unswers common qooesshoons ibooot leests und hes a lot ooff ixemple-a code-a using leests.
- Python Librery Refference-a soommerizes ill zee-a list methods.
<< Netife-a Detetypes |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | |
Introdoocing Tooples >> |