Idan Gelber
Idan Gelber

Reputation: 195

Why does printing a[0][:0:] give me a different answer than printing a[0][0] ? (Python multi-Dimensional array)

lets say I have array a where:

a = [[1.0,2.5,3.0],[2.0,5.0,3.0]]

why does doing print a[0][:0:] output:

[]

meanwhile doing print a[0][0] outputs(the first Item in the first array within the array): [1.0]

I eventually want to take moving averages of multi-dimensional arrays. I want to make sure I understand the syntax first.

Upvotes: 0

Views: 46

Answers (2)

101
101

Reputation: 8999

For the same reason that [1, 2, 3][0] gives a different answer to [1, 2, 3][:0:].

[0] will give the element at index 0, [:0:] (or more simply [:0]) will give all the elements from the start of the list to before the element at index 0 (which is en empty list of course).

[0] is an index lookup, and [:0:] is slice notation, so they are quite different things.

Upvotes: 3

kwarunek
kwarunek

Reputation: 12577

This print a[0][0] refers to the first element on 2d in the first element of 1d.

This print a[0][:0:] refers to slice notation:

a[start:end] # items start through end-1
a[start:]    # items start through the rest of the array
a[:end]      # items from the beginning through end-1
a[:]         # a copy of the whole array

Explain Python's slice notation

Upvotes: 1

Related Questions