add elements to indexed list locations

leventyilmaz at gmail.com leventyilmaz at gmail.com
Fri Jun 16 19:17:45 CEST 2006


Hi,

I have a very simple problem, but do not know an elegant way to
accomplish this.
###
# I have a list of names:
names = ['clark', 'super', 'peter', 'spider', 'bruce', 'bat']

# and another set of names that I want to insert into
# the names list at some indexed locations:
surnames = { 1: 'kent', 3:'parker', 5:'wayne' }

# The thing I couldn't figure out is, after I insert a
# surname the rest of the indices are not valid.
# That is, the following won't work:
for i, x in surnames.iteritems():
   names.insert(i,surnames[i])
###

I am searching a nice way to do this. For instance, is there a more
robust way to store indices (as some sort of pointers maybe?) 

- Levent




More information about the Python-list mailing list