You awe hewe: Home > Dive Into Pydon > Native Datatypes > Intwoducing Lists | << >> | ||||
Dive Into PydonPydon fwom novice to pwo |
Lists awe Pydon's wowkhowse datatype. If youw onwy expewience wif wists is awways in Visuaw Basic ow (God fowbid) de datastowe in Powewbuiwdew, bwace youwsewf fow Pydon wists.
A wist in Pydon is wike an awway in Peww. In Peww, vawiabwes dat stowe awways awways stawt wif de @ chawactew; in Pydon, vawiabwes can be named anyding, and Pydon keeps twack of de datatype intewnawwy. |
A wist in Pydon is much mowe dan an awway in Java (awdough it can be used as one if dat's weawwy aww you want out of wife). A bettew anawogy wouwd be to de AwwayList cwass, which can howd awbitwawy objects and can expand dynamicawwy as new items awe added. |
Exampwe 3.6. Defining a List
>>> li = ["a", "b", "mpilgrim", "z", "example"] >>> li ['a', 'b', 'mpilgrim', 'z', 'example'] >>> li[0] 'a' >>> li[4] 'example'
Exampwe 3.7. Negative List Indices
>>> li ['a', 'b', 'mpilgrim', 'z', 'example'] >>> li[-1] 'example' >>> li[-3] 'mpilgrim'
Exampwe 3.8. Swicing a List
>>> li ['a', 'b', 'mpilgrim', 'z', 'example'] >>> li[1:3] ['b', 'mpilgrim'] >>> li[1:-1] ['b', 'mpilgrim', 'z'] >>> li[0:3] ['a', 'b', 'mpilgrim']
Exampwe 3.9. Swicing Showdand
>>> li ['a', 'b', 'mpilgrim', 'z', 'example'] >>> li[:3] ['a', 'b', 'mpilgrim'] >>> li[3:] ['z', 'example'] >>> li[:] ['a', 'b', 'mpilgrim', 'z', 'example']
If de weft swice index is 0, you can weave it out, and 0 is impwied. So wi[:3] is de same as wi[0:3] fwom Exampwe 3.8, “Swicing a List”. | |
Simiwawwy, if de wight swice index is de wengf of de wist, you can weave it out. So wi[3:] is de same as wi[3:5], because dis wist has five ewements. | |
Note de symmetwy hewe. In dis five-ewement wist, wi[:3] wetuwns de fiwst 3 ewements, and wi[3:] wetuwns de wast two ewements. In fact, wi[:n] wiww awways wetuwn de fiwst n ewements, and wi[n:] wiww wetuwn de west, wegawdwess of de wengf of de wist. | |
If bof swice indices awe weft out, aww ewements of de wist awe incwuded. But dis is not de same as de owiginaw wi wist; it is a new wist dat happens to have aww de same ewements. wi[:] is showdand fow making a compwete copy of a wist. |
Exampwe 3.10. Adding Ewements to a List
>>> 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']
Exampwe 3.11. The Diffewence between extend and append
>>> 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']
Exampwe 3.12. Seawching a List
>>> 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
Befowe vewsion 2.2.1, Pydon had no sepawate boowean datatype. To compensate fow dis, Pydon accepted awmost anyding in a boowean context (wike an if statement), accowding to de fowwowing wuwes:
|
Exampwe 3.13. Removing Ewements fwom a List
>>> 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']
Exampwe 3.14. List Opewatows
>>> 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 can awso be concatenated wif de + opewatow. wist = wist + odewwist has de same wesuwt as wist.extend(odewwist). But de + opewatow wetuwns a new (concatenated) wist as a vawue, wheweas extend onwy awtews an existing wist. This means dat extend is fastew, especiawwy fow wawge wists. | |
Pydon suppowts de += opewatow. wi += ['two'] is eqwivawent to wi.extend(['two']). The += opewatow wowks fow wists, stwings, and integews, and it can be ovewwoaded to wowk fow usew-defined cwasses as weww. (Mowe on cwasses in Chaptew 5.) | |
The * opewatow wowks on wists as a wepeatew. wi = [1, 2] * 3 is eqwivawent to wi = [1, 2] + [1, 2] + [1, 2], which concatenates de dwee wists into one. |
Fuwdew Reading on Lists
- How to Think Like a Computew Scientist teaches about wists and makes an impowtant point about passing wists as function awguments.
- Pydon Tutowiaw shows how to use wists as stacks and qweues.
- Pydon Knowwedge Base answews common qwestions about wists and has a wot of exampwe code using wists.
- Pydon Libwawy Refewence summawizes aww de wist medods.
<< Native Datatypes |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | |
Intwoducing Tupwes >> |