3

Can anyone pleas tell what's difference between (if a is some numppy object, lets say:array) a[1] and a[[1]]. If I understand correctly I can edit 1 element of a with a[[1]], but not with a[1]...any other difference? Is it also something like that with [] I just view some element(s) and with [[]] I make copy? If it's to any importance I'm using python 3.

1 Answer 1

3

When you say a[1] you are getting the element with index 1 (the second one) from a. a[[1]], on the other hand, is not a special syntax, it just means "get the elements of a indicated by the indices in the list [1]". You can also say a[[1, 2]], or use another array as index. The practical difference is that a[1] will be a single scalar element and a[[1]] will be an array with size 1 (assuming a had one dimension).

With respect to assignment, you will find that both a[1] = 2 and a[[1]] = 2 work equally well. This is because of NumPy's broadcasting semantics, which allow you to match objects of different dimensions seamlessly. However, the actual dimensions of a[1] and a[[1]] are different, and it does matter depending on the context.

You can read more about indexing in NumPy here.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.