insertion in lists

L

Lupe

hi,

I'm trying to have a kind of multi-dimensional list which seems to me to be
the best way to have an array of not known extension, at the begining of
the program.

if I have:
element

list [0]
and want to have list[0][0]
list[0][1]

how can I insert the element to that list[0][0], for example?
just to put things clearer, if I wanted to insert the element in list[0] I
could do list.insert(0,element).

I've tried list[0].insert(0,element) but gives me an error.


Luis
 
W

wes weston

Lupe said:
hi,

I'm trying to have a kind of multi-dimensional list which seems to me to be
the best way to have an array of not known extension, at the begining of
the program.

if I have:
element

list [0]
and want to have list[0][0]
list[0][1]

how can I insert the element to that list[0][0], for example?
just to put things clearer, if I wanted to insert the element in list[0] I
could do list.insert(0,element).

I've tried list[0].insert(0,element) but gives me an error.


Luis

Luis,
Is this what you want?
>>> list = []
>>> list.insert(0,[])
>>> list[0].insert(0,5)
>>> list
[[5]]

wes
 
R

rahuel

Lupe said:
hi,

I'm trying to have a kind of multi-dimensional list which seems to me to
be the best way to have an array of not known extension, at the begining
of the program.

if I have:
element

list [0]
and want to have list[0][0]
list[0][1]

how can I insert the element to that list[0][0], for example?
just to put things clearer, if I wanted to insert the element in list[0] I
could do list.insert(0,element).

I've tried list[0].insert(0,element) but gives me an error.


Luis

Hi Luis,

Are you sure you're trying to code some python ????

To define a list named myList then :
myList=[]

To append myElement to this list :
myList.append(myElement)

That's it

Bye,
 
L

Lupe

wes said:
hi,

I'm trying to have a kind of multi-dimensional list which seems to me to
be the best way to have an array of not known extension, at the begining
of the program.

if I have:
element

list [0]
and want to have list[0][0]
list[0][1]

how can I insert the element to that list[0][0], for example?
just to put things clearer, if I wanted to insert the element in list[0]
I could do list.insert(0,element).

I've tried list[0].insert(0,element) but gives me an error.


Luis

Luis,
Is this what you want?
list = []
list.insert(0,[])
list[0].insert(0,5)
list
[[5]]

wes

yes, it works for me!
Now I see why it wasn't working.
Thanks a lot!

Luis
 
L

Larry Bates

l=[]
l.append([])

now l=[[]] # That is this list contains a single empty list.

l.append([])

now l=[[],[]] # That is this list contains two empty lists.

l[0].append('a')

now l=[['a'],[]]

l[0].append('b')

now l=[['a','b'],[]]

l[0][0]='c'

now l=[['c'],'b'],[]]

You should be able to figure it out from here.

What is interesting is that your list can have arbitrary
elements that may be lists, strings, dictionaries, classes,
etc. and they don't have to have anything in common.

More complex example:

class x:
pass

l=['test',{'key1':1,'key2':2}, 1.5, [1,2,3,5,6,7,8], x]

Has string at l[0]
Has dictionary at l[1]
Has float at l[2]
Has another list at l[3]
Has a class reference at l[4]

WARNING-Never use 'list' as a variable name (as you did
in your example) , it is a keyword and will get redefined
by the assignment (without warning).

-Larry
 
G

Greg Ewing (using news.cis.dfn.de)

Lupe said:
I'm trying to have a kind of multi-dimensional list which seems to me to be
the best way to have an array of not known extension, at the begining of
the program.

You might also like to consider using a dictionary indexed
by a tuple, which behaves like a sparse array, so you can
do things like

d = {}
d[0, 0] = 'a'
d[5, 7] = 'b'

without having to worry about pre-allocating lists and
sub-lists of the right lengths first.
 
L

Lupe

Python is not only a great language but also have excellent people helping
other


Luis
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,183
Messages
2,570,969
Members
47,524
Latest member
ecomwebdesign

Latest Threads

Top