{"id":3731,"date":"2018-07-30T22:12:49","date_gmt":"2018-07-30T16:42:49","guid":{"rendered":"http:\/\/codetheory.in\/?p=3731"},"modified":"2018-07-30T22:12:49","modified_gmt":"2018-07-30T16:42:49","slug":"how-to-access-the-elements-of-a-generator-object-in-python","status":"publish","type":"post","link":"https:\/\/codetheory.in\/how-to-access-the-elements-of-a-generator-object-in-python\/","title":{"rendered":"How to access the elements of a generator object in Python"},"content":{"rendered":"

A generator object in the Python computer programming language is totally different from a normal function, as it returns multiple values one by one through the yield statement. <\/p>\n

As you may already know, a normal function in Python returns one value to the caller through the return statement. One can easily access the value returned by the normal function just by calling it.<\/p>\n

What about accessing the values of a generator object in the Python computer programming language? What approach should the coder take to get the values returned by the generator function?<\/p>\n

Let’s build a generator function first, and later access its values with the help of a Python for loop statement.<\/p>\n

Let’s build a generator function<\/h2>\n

The coder can easily code a generator function by making use of the Python’s def statement which is being used to define normal functions.<\/p>\n

Let’s define the skeleton of the generator function first. Make sure to launch a fresh interactive console in your own operating system so you can practice the code being shared in here.<\/p>\n

\r\ndef gen_func():\r\n    pass\r\n<\/pre>\n

For now, the above functions does not do anything. It is just a skeleton for our generator.<\/p>\n

Let’s define a local list inside the above function; and insert some elements in it.<\/p>\n

\r\ndef gen_function():\r\n    l = [1, 2, 3, 4, 5, 6, 7, 8,  9, 10,]\r\n    return l\r\n<\/pre>\n

As you can see from the above piece of Python code, the function gen_function defines a normal function and returns a list object to the caller.<\/p>\n

Let’s call the above function in the Python’s interactive console and see the result the comes out.<\/p>\n

\r\ng = gen_functiom()\r\nprint(g)\r\n<\/pre>\n

Once I managed to execute the above piece of Python code in my interactive console, I got back the following output.<\/p>\n

\r\n[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\r\n<\/pre>\n

At the moment, the function gen_function is a normal one. It is not a generator yet.<\/p>\n

Let’s customize the gen_function so we can build a generator out of it.<\/p>\n

\r\ndef gen_function():\r\n    l = [1, 2, 3, 4, 5, 6, 7, 8,  9, 10,]\r\n    for el in l:\r\n        yield el\r\n<\/pre>\n

After you have managed to customize the code so it looks like the one being shown above, call the function gen_function again with the help of the following command.<\/p>\n

\r\ngen_function()\r\n<\/pre>\n

If you have customized the gen_function the right way, you should get the following output.<\/p>\n

\r\n<generator object gen_function at 0x108660a00>\r\n<\/pre>\n

Now let’s try to access the values of our generator object by making use of a for loop.<\/p>\n

\r\ngen_obj = gen_function()\r\nfor el in gen_obj:print(el)\r\n<\/pre>\n

After executing the above piece of Python code, you should get the following output.<\/p>\n

\r\n1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n<\/pre>\n

Final thoughts<\/h2>\n

As you can see from the examples being shared through this blog post, we can easily access the results of a generator object with the help of a for loop statement.<\/p>\n","protected":false},"excerpt":{"rendered":"

A generator object in the Python computer programming language is totally different from a normal function, as it returns multiple values one by one through the yield statement. As you may already know, a normal function in Python returns one value to the caller through the return statement. One can easily access the value returned … Continue reading “How to access the elements of a generator object in Python”<\/span><\/a><\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[],"_links":{"self":[{"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/posts\/3731"}],"collection":[{"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/comments?post=3731"}],"version-history":[{"count":3,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/posts\/3731\/revisions"}],"predecessor-version":[{"id":3734,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/posts\/3731\/revisions\/3734"}],"wp:attachment":[{"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/media?parent=3731"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/categories?post=3731"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/tags?post=3731"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}