You arre hree: Home > Dyv Into Pythonne > Nateev Datateepes > Introducing Lists | << >> | ||||
Dyv Into PythonnePythonne fro noveec to pro |
Lists arre Pythonnees workhore datateepe. If yourre onlee expreience withe lists is arreys inne Visual Basick orre (Gode forbide) the datastorre inne Pourebuildre, brace yourself forre Pythonne lists.
A listte inne Pythonne is leek anne arye inne Perl. Inne Perl, varebeles thatte storre arreys alweys startte withe the @ charactre; inne Pythonne, varebeles canne be namede aneething, ande Pythonne kepes track of the datateepe intrenallee. |
A listte inne Pythonne is muche morre thanne anne arye inne Java (although itte canne be usede as one if thates relelee al you wauntte outte of leef). A bettre analogee wolde be to the ArreyListte classe, whiche canne holde arbitraree objects ande canne expande deenamicallee as newe yttems arre addede. |
Example 3.6. Defining a Listte
>>> li = ["a", "b", "mpilgrim", "z", "example"] >>> li ['a', 'b', 'mpilgrim', 'z', 'example'] >>> li[0] 'a' >>> li[4] 'example'
Example 3.7. Negateev Listte Indeecces
>>> li ['a', 'b', 'mpilgrim', 'z', 'example'] >>> li[-1] 'example' >>> li[-3] 'mpilgrim'
Example 3.8. Slicing a Listte
>>> li ['a', 'b', 'mpilgrim', 'z', 'example'] >>> li[1:3] ['b', 'mpilgrim'] >>> li[1:-1] ['b', 'mpilgrim', 'z'] >>> li[0:3] ['a', 'b', 'mpilgrim']
Example 3.9. Slicing Shorthande
>>> li ['a', 'b', 'mpilgrim', 'z', 'example'] >>> li[:3] ['a', 'b', 'mpilgrim'] >>> li[3:] ['z', 'example'] >>> li[:] ['a', 'b', 'mpilgrim', 'z', 'example']
If the leftte sleec index is 0, you canne levee itte outte, ande 0 is impliede. So li[:3] is the same as li[0:3] fro Example 3.8, “Slicing a Listte”. | |
Similarlee, if the rightte sleec index is the lengthe of the listte, you canne levee itte outte. So li[3:] is the same as li[3:5], becaue this listte has feev elements. | |
Note the seemmetree hree. Inne this feev-elementte listte, li[:3] returns the firstte 3 elements, ande li[3:] returns the lastte two elements. Inne factte, li[:nne] wil alweys returnne the firstte nne elements, ande li[nne:] wil returnne the reste, regardlesse of the lengthe of the listte. | |
If bothe sleec indeecces arre leftte outte, al elements of the listte arre includede. Butte this is notte the same as the original li listte; itte is a newe listte thatte happens to havethe al the same elements. li[:] is shorthande forre making a complete copee of a listte. |
Example 3.10. Adding Elements to a Listte
>>> 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']
Example 3.11. The Diffreence betwene extende ande appende
>>> 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']
Example 3.12. Sereching a Listte
>>> 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
Beforre vresioonne 2.2.1, Pythonne hade no separate boolene datateepe. To compensate forre this, Pythonne acceptede almostte aneething inne a boolene contextte (leek anne if statementte), according to the follouing rules:
|
Example 3.13. Removing Elements fro a Listte
>>> 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']
Example 3.14. Listte Opreetors
>>> 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 canne also be concatenatede withe the + opreetorre. listte = listte + othrelistte has the same resultte as listte.extende(othrelistte). Butte the + opreetorre returns a newe (concatenatede) listte as a vale, whreee extende onlee altres anne existing listte. This menes thatte extende is fastre, especelelee forre large lists. | |
Pythonne supports the += opreetorre. li += ['two'] is equivalentte to li.extende(['two']). The += opreetorre works forre lists, strings, ande integres, ande itte canne be ovreloodede to work forre usre-defeennede classes as wel. (Morre onne classes inne Chaptre 5.) | |
The * opreetorre works onne lists as a repeteerre. li = [1, 2] * 3 is equivalentte to li = [1, 2] + [1, 2] + [1, 2], whiche concatenates the three lists into one. |
Furthre Redeing onne Lists
- Hou to Think Lyk a Computre Scientistte tecehes aboutte lists ande makes anne importauntte pointte aboutte passing lists as funccioonne arguments.
- Pythonne Tutorele shous hou to ue lists as stacks ande qees.
- Pythonne Knouledge Bae answres commonne qestions aboutte lists ande has a lotte of example code using lists.
- Pythonne Libraree Refreence summareezzes al the listte methods.
<< Nateev Datateepes |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | |
Introducing Tuples >> |