What does the optional parameter in this for-loop in Python mean?

I came across a cycle that is unusual for me. What does method mean in this for-loop?

for method, config in self.myList.items():

+6
source share
2 answers

items() is the method used for python dictionaries to return iterable holding tuples for each dictionary keys and their corresponding value .

In Python, you can unpack lists and tuples into variables using the method you showed.

eg:.

 item1, item2 = [1,2] # now we have item1 = 1, item2 = 2 

Therefore, if self.myList is a dict , method and config will refer to key and value in each tuple for this iteration, respectively.

If self.myList not a dict , I would suggest that it either inherits from dict , or the items() method is similar (you would know better).

+7
source

It unpacks the tuple returned from the call to items() into the method and config variables.

+4
source

Source: https://habr.com/ru/post/985198/


All Articles