# The difference in usage between nn.ModuleList and python list

**URL:** <https://discuss.pytorch.org/t/the-difference-in-usage-between-nn-modulelist-and-python-list/7744>\
**Category:** Uncategorized\
**Created:** [September 23, 2017, 4:20am UTC](https://discuss.pytorch.org/t/the-difference-in-usage-between-nn-modulelist-and-python-list/7744 "2017-09-23T04:20:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sirius27](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@sirius27](https://discuss.pytorch.org/u/sirius27)\
**Post date:** [September 23, 2017, 4:20am UTC](https://discuss.pytorch.org/t/the-difference-in-usage-between-nn-modulelist-and-python-list/7744/1 "2017-09-23T04:20:46Z")

</div>

I can’t figure out what the purpose of nn.ModuleList is because as far as I know, list can be used in most scenarios I come up with.  
For example, for the newtork structure below,

```auto
	def __init__ (self):
		super(MyNN, self). __init__ ()
		fcs = [nn.Sequential(
				 nn.Linear(fc_input_size, fc_hidden_size),
				 nn.ReLU(),
				 nn.Linear(fc_hidden_size,num_classes)
		) for fc_hidden_size in fc_hidden_sizes]
		#self.network = nn.ModuleList(fcs)
		self.network = fcs

```

I don’t see the difference between list and nn.ModuleList.  
Can anyone please give an example of where nn.ModuleList has advantage and explain it’s designed purpose?

---

<div class="post-metadata">

**Author:** ![smth](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/smth/32/13_2.png) [@smth](https://discuss.pytorch.org/u/smth)\
**Post date:** [September 28, 2017, 3:26pm UTC](https://discuss.pytorch.org/t/the-difference-in-usage-between-nn-modulelist-and-python-list/7744/2 "2017-09-28T15:26:34Z")

</div>

When you ask for `model.parameters()`, the parameters of layers inside `nn.ModuleList` will be returned.  
But if it is regular list, we dont look inside the list for `nn.Module` layers.

In your example, if you do:

`list(MyNN().parameters())` then you will see that the weight of the nn.Linear in `self.network` will be missing.

---

<div class="post-metadata">

**Author:** ![sirius27](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@sirius27](https://discuss.pytorch.org/u/sirius27)\
**Post date:** [October 9, 2017, 1:48am UTC](https://discuss.pytorch.org/t/the-difference-in-usage-between-nn-modulelist-and-python-list/7744/3 "2017-10-09T01:48:08Z")

</div>

I tried what you suggested, and it’s indeed the case. Thanks a lot!

---

<div class="post-metadata">

**Author:** ![trangham283](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/trangham283/32/6734_2.png) [@trangham283](https://discuss.pytorch.org/u/trangham283)\
**Post date:** [September 12, 2018, 11:10pm UTC](https://discuss.pytorch.org/t/the-difference-in-usage-between-nn-modulelist-and-python-list/7744/4 "2018-09-12T23:10:05Z")

</div>

I’m late to the party, but does this mean using `list` instead of `ModuleList` will be wrong? i.e. will the weights of my networks inside the list still be updated during training?

---

<div class="post-metadata">

**Author:** ![InnovArul](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/innovarul/32/5282_2.png) [@InnovArul](https://discuss.pytorch.org/u/InnovArul)\
**Post date:** [September 13, 2018, 1:02am UTC](https://discuss.pytorch.org/t/the-difference-in-usage-between-nn-modulelist-and-python-list/7744/5 "2018-09-13T01:02:32Z")

</div>

Yes the weights of the modules inside the python list will not be updated in training, unless you manually add them to the list of parameters passed to the optimizer. Moreover, even if you do that, when you want to save the model parameters using `model.state_dict()`, the parameters of modules inside the python list won’t be saved.

---

<div class="post-metadata">

**Author:** ![Abhishek\_Kishore](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/abhishek_kishore/32/12027_2.png) [@Abhishek\_Kishore](https://discuss.pytorch.org/u/Abhishek_Kishore)\
**Post date:** [May 17, 2020, 12:24pm UTC](https://discuss.pytorch.org/t/the-difference-in-usage-between-nn-modulelist-and-python-list/7744/6 "2020-05-17T12:24:54Z")

</div>

In Training a classifier example of pytorch 60 min blitz tutorial the modules are not added in Modulelist still the optimizer is able to access the parameters. Why is that?

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [May 18, 2020, 7:09am UTC](https://discuss.pytorch.org/t/the-difference-in-usage-between-nn-modulelist-and-python-list/7744/7 "2020-05-18T07:09:34Z")

</div>

Double post from [here](https://discuss.pytorch.org/t/is-it-mandatory-to-add-modules-to-modulelist-to-access-its-parameters/81622/2) with a potential answer.
