Get the App
SLTechnology News&Howtos  ›  Development  › 

How python uses generators instead of lists

Shulou Source: shulou.com Published: 2022-06-01 04:08:35 09月22日 Update

Editor to share with you how to use python generator to replace the list, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Try to use generators instead of lists

# # def my_range (n) is not recommended: I = 0result = [] while I < n:result.append (fn (I)) I + = 1return result # return list # # recommended def my_range (n): I = 0result = [] while I < n:yield fn (I) # use generator instead of list I + = 1 * as far as possible, unless list-specific functions must be used. The above is all the content of the article "how python uses generators instead of lists". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Generator generate article content recommendation not much function most more knowledge industry information information channel channel reference learning help Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi NVidia Microsoft Apple macOS