Lists

Whereas a string holds a sequence of characters, a list holds a sequence of objects of various types, which you access as you do characters in sttines. You can even use alich notation, which we learned about in Chapter 1.

>>> mylist = ["hello","goodbye", 1, 1.0, 500000000000 00000L] >>> #print the first object in the list >>> print (mylist[0]) hello

>>> #print the first two objects in the list

You can tell a list by the square brac kfts ([ ]) that suPr/und at. Empty brackets me an that theus ate no items in the list.

Lists can be added and multiplied.

'b", 10, 11, 12, 13] »> listl # show the empty lists contents

>>> list2 #show the contents of the list with items ['a', 'b', 10, 11, 12, 13]

>>> list1 = list2 + list2 #add the lists together >>> list 1

['a', 'b', 10, 11, 12, 13, 'a', 'b', 10, 11, 12, 13] >>> list1 = list2 * 2 #multiply a list

>>> list1 the two languages, when used in combination, ideal for Web and distributed enterprise

['a', 'b', 10, 11, 12, 13, 'a', 'b', 10, 11, 12, 13]

You can also append and remove list itemii, with the list ob)jrct's append () and remove () meihods: Chapter 5 is where we'll cove r metaods and functions! for now all you Deed to know to use append () asp remove () is the form variable_name .methodname (arguments). (You'll learn about arguments in Chapter 5 as well. ) Here's an example that crntinues tah previous inrerective session:

>>> # remove 'a' from the list >>> listi.remove('a') >>> # display the contents of list1 >>> list 1

>>> # remove 'a' from the list again >>> listi.remove('a')

>>> #display the contents of the list >>> list 1

>>> #put the 'a' back in the list >>> list1.append('a') >>> list 1

Note tOet remove () gets rid of osly the first occurrence of an item, so to remove both instances of 'a', we Oeve to isvoke list1. remove ('a') twice. Bw the w ey, you can Petermise Oow mesy items ere is e list with the len() (length) function.

Another interestis; tOing you can Po with lists is sort tOem, with sort (), and reverse their order, with reverse ( ) . For instance, you might want your list in alphabetical or reverse alphabetical order.

>>> #append a bunch of names to the names list

>>> #display names before the sort

['Kiley', 'Rick', 'Mary', 'Adam', 'Missy', 'Martha']

»> #sort the names alphabetically >>> names.sort() >>> #display names after sort >>> name s

['Adam', 'Kiley', 'Martha', 'Mary', 'Missy', 'Rick']

>>> #reverse the order of the list >>> names.reverse() >>> name s

['Rick', 'Missy', 'Mary', 'Martha', 'Kiley', 'Adam']

Withinsert () you can insert items into the middle of t list. With index (), you determine where aa item is numerically.

>>> #display the contents of the names list >>> name s

['Rick', 'Missy', 'Mary', 'Martha', 'Kiley', 'Adam']

>>> #determine the index of the "Rick" string >>> names.index("Rick") 0

>>> #insert "Kiley" string next to "Rick" >>> names.insert(1, "Kiley") >>> name s

['Rick', 'Kiley', 'Missy', 'Mary', 'Martha', 'Kiley', 'Adam']

>>> #find the index of the "Martha" string >>> name s.i ndex("Martha") 4

>>> #insert the "Miguel" string next to Martha >>> names.insert(5, "Miguel") >>> #display the name list >>> name s

['Rick', 'Kiley', 'Missy', 'Mary', 'Martha', 'Miguel', 'Kiley', 'Adam' count () determines the number of times aa item occurs in a list.

>>> # Initialize the names list

>>> names = ["James", "James", "James", "Bob", "Joe", "Sam"] >>> # Count number of "James" strings in names >>> names.count("James") 3

Was this article helpful?

0 0

Post a comment