Faqts : Computers : Programming : Shopping For You : Python : Common Problems : Lists

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

6 of 6 people (100%) answered Yes
Recently 6 of 6 people (100%) answered Yes

Entry

Is it possible to have a list that automatically extends when I assign to a non-existant element?

Mar 1st, 2009 16:50
osososo didididi, Daniel Gonzalez Gasull, games games, Michael Chermside, Ed Parcell,


The answer, of course, is "yes", but HOW to do it depends on your exact
needs. [NOTE: All code shown here has been tested in Python 2.3]
The answer that suits most often may actually surprise you -- use a
dictionary instead of a list! This may sound odd, but it works quite
nicely for a large range of applications (the language PHP uses this
approach for its lists). Code like this:
    >>> sparseList = {}
    >>> for i in range(10):
    ...     sparseList[i] = i
    ...
    >>> sparseList[99] = 99
demonstrates populating part of the "list" completely, and setting a
"non-existant" element. The biggest problem with using a dict instead 
of
a list is that looping through the dict doesn't happen in order... but
that's easy to fix with a tiny helper function:
    >>> sparseList[99] = 99
    >>> def sorted(li):
    ...     li.sort()
    ...     return li
    ...
    >>> for i in sorted(sparseList.keys()):
    ...     print i,
    ...
    0 1 2 3 4 5 6 7 8 9 99
The dict approach has several advantages -- it uses nothing but a
built-in data type, it provides fairly speedy access to list elements,
and it uses no memory for "empty" array items. The last point makes it
particularly good if you expect long streatches of empty slots (thus
it's perfect for "sparse" list applications).
But there may be reasons why you need a "real" list. Perhaps you expect
to have all or almost all of the slots in the array filled, you just
won't be filling them in left-to-right order. Perhaps your application
is near the limits of its performance and you need the slightly-faster
performance of true lists (smaller constants for constant-time
per-element access, and less memory used per occupied list element). In
that case, what you need is really a NORMAL list that just extends
itself automagically. Python doesn't provide such a thing, but it DOES
allow you to extend the built-in list class. Here's one (very simple)
approach:
    >>> class AutoExtendList(list):
    ...     def __setitem__(self, key, value):
    ...         try:
    ...             list.__setitem__(self, key, value)
    ...         except IndexError:
    ...             empty_slots = [None]*(key-len(self))
    ...             self.extend(empty_slots)
    ...             self.append(value)
    ...
Now the list will automatically extend itself (with None) like this:
    >>> for i in range(10):
    ...     ael[i] = i
    ...
    >>> ael[14] = 14
    >>> for x in ael:
    ...     print x,
    ...
    0 1 2 3 4 5 6 7 8 9 None None None None 14
http://dl3.com/link/category/voice
http://dl3.com/link/category/chat
http://dl3.com/link/category/song
http://dl3.com/link
http://www.dl3.com/smiles
http://www.dl3.com/cards
http://games.dl3.com
http://www.chataluae.com
http://www.dl3.com/vb/index.php
http://www.dl3.com/vb
http://www.dl3.com/vb
http://www.dl3.com
http://www.stop10.com
http://topics.ll6.com
http://topics.ll6.com
http://www.stop10.com
http://www.ll6.com/java.shtml
http://dlel.ll6.com/c21.html
http://dlel.ll6.com/c20.html
http://dlel.ll6.com/c19.html
http://dlel.ll6.com/c18.html
http://dlel.ll6.com/c17.html
http://dlel.ll6.com/categories.xml
http://dlel.ll6.com/sitemap.xml
http://dlel.ll6.com/c10.html
http://dlel.ll6.com/c9.html
http://dlel.ll6.com/c8.html
http://dlel.ll6.com/c7.html
http://dlel.ll6.com/c6.html
http://dlel.ll6.com/c5.html
http://dlel.ll6.com/c4.html
http://dlel.ll6.com/c3.html
http://dlel.ll6.com/c2.html
http://dlel.ll6.com/c10.html
http://dlel.ll6.com/c16.html
http://dlel.ll6.com/c15.html
http://dlel.ll6.com/c14.html
http://dlel.ll6.com/c13.html
http://dlel.ll6.com/c12.html
http://dlel.ll6.com/c11.html
http://voice.ll6.com
http://www.ll6.com
http://vb.ll6.com/forumdisplay.php?f=50
http://vb.ll6.com/forumdisplay.php?f=41
http://vb.ll6.com/forumdisplay.php?f=35
http://vb.ll6.com/forumdisplay.php?f=34
http://vb.ll6.com/forumdisplay.php?f=33
http://vb.ll6.com/forumdisplay.php?f=32
http://vb.ll6.com/forumdisplay.php?f=31
http://vb.ll6.com/forumdisplay.php?f=30
http://vb.ll6.com/forumdisplay.php?f=29
http://vb.ll6.com/forumdisplay.php?f=28
http://vb.ll6.com/forumdisplay.php?f=27
http://vb.ll6.com/forumdisplay.php?f=26
http://vb.ll6.com/forumdisplay.php?f=25
http://vb.ll6.com/forumdisplay.php?f=24
http://vb.ll6.com/forumdisplay.php?f=23
http://vb.ll6.com/forumdisplay.php?f=22
http://vb.ll6.com/forumdisplay.php?f=21
http://vb.ll6.com/forumdisplay.php?f=20
http://vb.ll6.com/forumdisplay.php?f=19
http://vb.ll6.com/forumdisplay.php?f=18
http://vb.ll6.com/forumdisplay.php?f=17
http://vb.ll6.com/forumdisplay.php?f=5
http://vb.ll6.com/forumdisplay.php?f=6
http://vb.ll6.com/forumdisplay.php?f=10
http://vb.ll6.com/forumdisplay.php?f=4
http://vb.ll6.com/index.php
http://ll6.com
http://www.alshalah.com
http://www.alshalah.com/chat1420.html
http://www.alshalah.com/music22.html
http://www.alshalah.com/music21.html
http://www.alshalah.com/chat1355.html
http://www.alshalah.com/chat1507.html
http://www.alshalah.com/download7.html
http://www.alshalah.com/download7.html
http://www.alshalah.com/chat1337.html
http://www.alshalah.com/chat100.html
http://www.alshalah.com/music12.html
http://www.alshalah.com/music12.html
http://www.alshalah.com/download5.html
http://www.alshalah.com/music23.html
http://www.alshalah.com/chat1339.html
http://www.alshalah.com/chat103.html
http://www.alshalah.com/music15.html
http://www.alshalah.com/chat1528.html
http://www.alshalah.com/chat1359.html
http://www.alshalah.com/chat1519.html
http://www.alshalah.com/chat1422.html
http://www.alshalah.com/music4.html
http://www.alshalah.com/chat101.html
http://www.alshalah.com/chat106.html
http://www.alshalah.com/chat102.html
http://www.alshalah.com/chat1166.html
http://www.alshalah.com/vote.php
http://www.alshalah.com/visit.php
http://www.alshalah.com/chat2821.html
http://www.alshalah.com/chat2821.html
http://www.alshalah.com/chat2821.html
http://www.alshalah.com/chat2821.html
http://www.alshalah.com/chat2820.html
http://www.alshalah.com/chat2820.html
http://www.alshalah.com/chat2819.html
http://www.alshalah.com/chat2819.html
http://www.alshalah.com/chat2818.html
http://www.alshalah.com/chat2818.html
http://www.alshalah.com/chat2817.html
http://www.alshalah.com/chat2817.html
http://www.alshalah.com/chat2816.html
http://www.alshalah.com/chat2816.html
http://www.alshalah.com/chat2815.html
http://www.alshalah.com/chat2815.html
http://www.alshalah.com/chat2814.html
http://www.alshalah.com/chat2814.html
http://www.alshalah.com/chat2813.html
http://www.alshalah.com/chat2813.html
http://www.alshalah.com/chat2812.html
http://www.alshalah.com/chat2812.html
http://www.alshalah.com/chat2813.html
http://www.alshalah.com/chat2811.html
http://www.alshalah.com/chat2811.html
http://www.alshalah.com/chat2810.html
http://www.alshalah.com/chat2810.html
http://www.alshalah.com/chat2809.html
http://www.alshalah.com/chat2809.html
http://www.alshalah.com/chat2808.html
http://www.alshalah.com/chat2808.html
http://www.alshalah.com/chat2807.html
http://www.alshalah.com/chat2807.html
http://www.alshalah.com/chat2806.html
http://www.alshalah.com/chat2806.html
http://www.alshalah.com/chat2805.html
http://www.alshalah.com/chat2804.html
http://www.alshalah.com/chat2803.html
http://www.alshalah.com/chat2803.html
http://www.alshalah.com/chat2802.html
http://www.alshalah.com/voice
http://www.alshalah.com/vb
http://www.alshalah.com/
http://www.alshalah.com/download3.html
http://www.alshalah.com/music6.html
http://www.alshalah.com/music6.html
http://www.alshalah.com/music5.html
http://www.alshalah.com/music5.html
http://www.alshalah.com/music4.html
http://www.alshalah.com/music4.html
http://www.alshalah.com/music3.html
http://www.alshalah.com/music3.html
http://www.alshalah.com/music22.html
http://www.alshalah.com/music21.html
http://www.alshalah.com/music19.html
http://www.alshalah.com/music18.html
http://www.alshalah.com/vb/sex202
http://www.alshalah.com/vb/sex205
http://www.alshalah.com/vb/sitemap
http://www.alshalah.com/vb/sitemap/f-183.html
http://www.alshalah.com/chat101.html
http://www.alshalah.com/chat101.html
http://www.alshalah.com/vb/picturet38947.html
http://www.alshalah.com/vb/picturet25902.html
http://www.alshalah.com/chat2801.html
http://www.alshalah.com/chat2800.html
http://www.alshalah.com/chat2799.html
http://www.alshalah.com/download10.html
http://www.alshalah.com/chat259.html
http://www.alshalah.com/vb/picturet939.html
http://www.alshalah.com/vb/picturet7317.html
http://www.alshalah.com/chat102.html
http://www.alshalah.com/chat312.html
http://www.alshalah.com/vb/picturet706.html
http://www.alshalah.com/vb/picturet22974-2.html
http://www.alshalah.com/vb/picturet26805.html
http://www.alshalah.com/chat601.html
http://www.alshalah.com/vb/picturet29167.html
http://www.alshalah.com/vb/picturet21358.html
http://www.alshalah.com/vb/picturet42591.html
http://www.alshalah.com/vb/picturet10141.html
http://www.alshalah.com/chat960.html
http://www.alshalah.com/chat658.html
http://www.alshalah.com/chat102.html
http://www.alshalah.com/chat1166.html
http://www.alshalah.com/vb/picturet34614.html
http://www.alshalah.com/chat2470.html
http://www.alshalah.com/vb/picturet44915.html
http://www.alshalah.com/download5.html
http://www.alshalah.com/chat2408.html
http://www.alshalah.com/chat2408.html
http://www.alshalah.com/vb/picturet24559.html
http://www.alshalah.com/music11.html
http://www.alshalah.com/vb/picturet40695.html
http://www.alshalah.com/vb/picturet1067.html
http://www.stop10.com
http://www.alshalah.com/chat27.html
http://www.alshalah.com/vb/picturet10871.html
http://www.alshalah.com/vb/picturet46798.html
http://www.alshalah.com/vb/picturet4995.html
http://www.alshalah.com/vb/picturet4445.html
http://www.alshalah.com/vb/picturet17029.html
http://www.alshalah.com/chat2794.html
http://www.alshalah.com/vb/picturet42026.html
http://www.alshalah.com/vb/picturet28063.html
http://www.alshalah.com/vb/picturet27011.html
http://www.alshalah.com/vb/picturet3521.html
http://www.alshalah.com/vb/picturet25360.html
http://www.alshalah.com/chat2793.html
http://www.alshalah.com/chat886.html
http://www.alshalah.com/vb/picturet274.htmll
http://www.alshalah.com/vb/picturet274.html
http://www.alshalah.com/music16.html
http://www.alshalah.com/download7.html
http://www.alshalah.com/vb/picturet43943.html
http://www.alshalah.com/chat2666.html
http://www.alshalah.com/music28.html
http://www.alshalah.com/music28.html
http://www.alshalah.com/music15.html
http://www.alshalah.com/music28.html