Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could we always use the list type as the function parameter when the tuple type parameter is expected?

I have been studying the Tensorflow API written in Python. I have two questions.

1. Can we always use a list type as a function parameter when a tuple is expected?

If we look at the official API definition about the tf.placeholder and its examples (https://www.tensorflow.org/api_docs/python/tf/placeholder), we see that the second parameter of this function is the "shape". In the example code, we can see that a tuple is used to provide the shape information as a parameter as shown below.

x = tf.placeholder(tf.float32, shape=(1024, 1024))

However, in the official tutorial page (https://www.tensorflow.org/get_started/mnist/beginners), the example uses the list as the shape rather than using the tuple as shown below.

y_ = tf.placeholder(tf.float32, [None, 10])

I know that there are some differences between list and tuple such as being immutable vs mutable.

If the list supports all the functionality of a tuple, then could we always use the list instead of the tuple safely as a function parameter? And is it recommended?

2. What's the meaning of [None, 10] in the above example code?

In the above example code, [None, 10] is used. Are such expressions normally used? If so, then is "None" also considered as a kind of number?

like image 787
chanwcom Avatar asked Jan 27 '26 15:01

chanwcom


1 Answers

Almost everything which you can do on tuple you can do on list too. However the vice-versa is not true because tuple are immutable whereas list are mutable.

But there's exception. Since tuple is immutable:

  • it can be used as a key in a dictionary.
  • used in a set.

Lists are intended to be homogeneous sequences, while tuples are heterogeneous data structures. Also, tuple are little better in terms of performance.

From the Python's Tuples and Sequences document:

Though tuples may seem similar to lists, they are often used in different situations and for different purposes. Tuples are immutable, and usually contain a heterogeneous sequence of elements that are accessed via unpacking (see later in this section) or indexing (or even by attribute in the case of namedtuples).


So the answer to your question:

Can we always use a list type as a function parameter when a tuple is expected?

You may use list instead of tuple in most of the cases, but not always. But you need not to worry much about this, as Python will remind you when your usage of list may go wrong. Below is the error which you'll receive on doing so:

TypeError: unhashable type: 'list'

For example:

>>> set([1, [1, 2]])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'

>>> {[1, 2]: 1}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
like image 137
Moinuddin Quadri Avatar answered Jan 29 '26 05:01

Moinuddin Quadri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!