2

Python noob here. I need to store an array of float arrays. I am doing this but its not working out:

distance = [] ##declare my array
distance.append ([]) ##add an empty array to the array
distance[len(distance)-1].append ([0,1,2,3.5,4.2]) ## store array in array[0]
print distance[0][1] ## this doesnt work, the array above got stored as 1 item

4 Answers 4

3

Use list.extend not list.append:

The difference between extend and append is that append appends the object passed to it as it is. While extend expects that item passed to it to be an iterable(list, tuple,string, etc) and appends it's items to the list.

Using append we can append any type of object; i.e iterable or non-iterable.


>>> lis = [1,2,3]
>>> lis.append(4)      #non-iterable
>>> lis.append('foo')  #iterable
>>> lis
[1, 2, 3, 4, 'foo']

But extend behaves differently and actually appends the individual items from the iterable to the list.

>>> lis = [1,2,3]
>>> lis.extend('foo')      #string is an iterable in python
>>> lis
[1, 2, 3, 'f', 'o', 'o']   #extend appends individual characters to the list
>>> lis.extend([7,8,9])    #same thing happend here
>>> lis
[1, 2, 3, 'f', 'o', 'o', 7, 8, 9]
>>> lis.extend(4)          #an integer is an not iterable so you'll get an error
TypeError: 'int' object is not iterable

Your Code

>>> distance = [[]]
>>> distance[-1].extend ([0,1,2,3.5,4.2])
>>> distance
[[0, 1, 2, 3.5, 4.2]]

This returns:

[[0, 1, 2, 3.5, 4.2]]

If you wanted to do this then there's no need to append the empty [] and then call list.extend, just use list.append directly:

>>> ditance = [] ##declare my array
>>> distance.append([0,1,2,3.5,4.2])
>>> distance
[[0, 1, 2, 3.5, 4.2]]
Sign up to request clarification or add additional context in comments.

1 Comment

@fghajhe I've added some more explanation that you might find helpful.
2

Use extend instead of append:

distance[-1].extend([0,1,2,3.5,4.2])

(Also, note that distance[len(distance)-1] can be written distance[-1].)

Comments

1

You can also do this (since you already initialized an empty list to distance[0]):

distance[len(distance)-1] += [0,1,2,3.5,4.2]

Comments

1

Here's what you've done:

  1. Make a list
  2. Add a list to the list
  3. Add a list to the list in your list. If you've seen Inception, you know that you now have 3 lists, and the 3rd one has some items in it

There are several ways to accomplish your goal:

1.

distance[len(distance)-1].extend([0,1,2,3,4,5]) #this one has been addressed elseqhere
  1. This next one should make the most sense to you. Loop through and append to you inner list

    for item in [0,1,2,3,4]:
        distance[ -1 ].append( item)
    
  2. This last one is kind of cool and good to know, though really indirect here:

    map( lambda item : distance[0].append( item ), [1,2,3,4,5] )
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.