Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List and tuple behave differently

I'm well aware that there are differences between lists and tuples and that tuples aren't just constant lists, but there are few examples where the two are actually treated differently by the code (as opposed to by a coding convention), so I (sloppily) have used them interchangeably.

Then I came across a case where they give totally different behavior:

>>> import numpy as np
>>> a = np.arange(9).reshape(3,3)
>>> a
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
>>> idx = (1,1)
>>> a[idx]
4
>>> idx = [1,1]
>>> a[idx]
array([[3, 4, 5],
       [3, 4, 5]])

can someone explain what's going on here? More importantly, where else does this pitfall appear in scipy?

like image 440
Shep Avatar asked Aug 12 '13 06:08

Shep


People also ask

How are lists and tuples different?

The key difference between the tuples and lists is that while the tuples are immutable objects the lists are mutable. This means that tuples cannot be changed while the lists can be modified.

How a tuple differs with a list explain with suitable example?

One of the most important differences between list and tuple is that list is mutable, whereas a tuple is immutable. This means that lists can be changed, and tuples cannot be changed. So, some operations can work on lists, but not on tuples.

What is difference between list and tuple and string?

A string is a sequence of unicode characters with quotation marks on either side of the sequence. A tuple is an ordered sequence of objects or characters separated by commas with parentheses on either side of the sequence.


1 Answers

You are getting a different behavior because, in numpy, three types of indexing are supported

  • Basic Slicing
  • Advanced Indexing
  • Record Access

Using tuple for indexing is just equivalent to a parameter list, which suffixes as a Basic Slicing, where-as using a non-tuple like list results in Advanced Indexing.

Also remember, from the documentation

Advanced indexing is triggered when the selection object, obj, is a non-tuple sequence object, an ndarray (of data type integer or bool), or a tuple with at least one sequence object or ndarray (of data type integer or bool). There are two types of advanced indexing: integer and Boolean.

Advanced indexing always returns a copy of the data (contrast with basic slicing that returns a view).

And moreover, from the same documentation

In Python, x[(exp1, exp2, ..., expN)] is equivalent to x[exp1, exp2, ..., expN]; the latter is just syntactic sugar for the former.

like image 54
Abhijit Avatar answered Oct 24 '22 11:10

Abhijit