{"id":24850,"date":"2021-11-02T09:54:42","date_gmt":"2021-11-02T04:24:42","guid":{"rendered":"https:\/\/python-programs.com\/?p=24850"},"modified":"2021-11-02T09:54:42","modified_gmt":"2021-11-02T04:24:42","slug":"python-list-function-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-list-function-with-examples\/","title":{"rendered":"Python list() Function with Examples"},"content":{"rendered":"
list() Function in Python:<\/strong><\/p>\n The list() function is used to create a list object.<\/p>\n A list object is an ordered and changeable collection.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n iterable:<\/strong> This is required. It could be a sequence, collection, or iterator object.<\/p>\n Return Value:<\/strong><\/p>\n It returns a list.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n list() Function in Python: The list() function is used to create a list object. A list object is an ordered and changeable collection. Syntax: list(iterable) Parameters iterable: This is required. It could be a sequence, collection, or iterator object. Return Value: It returns a list. It returns an empty list if no parameters are passed. …<\/p>\nlist(iterable)<\/pre>\n
\n
Given tuple = (1, 2, 3, 4, 5)\r\nGiven string = \"hello\"\r\nGiven list = [10, 20, 30, 40]<\/pre>\n
The result after applying list() function on the given tuple = [1, 2, 3, 4, 5]\r\nThe result after applying list() function on the given string = ['h', 'e', 'l', 'l', 'o']\r\nThe result after applying list() function on the given list = [10, 20, 30, 40]<\/pre>\n
Given tuple = (25, 35, 45)\r\nGiven string = \"good\"\r\nGiven list = [1, 3, 5, 6]<\/pre>\n
The result after applying list() function on the given tuple = [25, 35, 45]\r\nThe result after applying list() function on the given string = ['g', 'o', 'o', 'd']\r\nThe result after applying list() function on the given list = [1, 3, 5, 6]<\/pre>\n
list() Function with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the tuple as static input and store it in a variable.\r\ngvn_tupl = (1, 2, 3, 4, 5)\r\n# Give the string as static input and store it in another variable.\r\ngvn_str = \"hello\"\r\n# Give the list as static input and store it in another variable.\r\ngvn_list = [10, 20, 30, 40]\r\n# Pass the given tuple as an argument to the list() function that converts a given\r\n# tuple to a list and returns a list.\r\n# Store it in another variable.\r\nrslt1 = list(gvn_tupl)\r\n# Print the result list after applying list() function on the given tuple.\r\nprint(\"The result after applying list() function on the given tuple = \", rslt1)\r\n# similarly do the same for the given string and list and print it.\r\nrslt2 = list(gvn_str)\r\nprint(\"The result after applying list() function on the given string = \", rslt2)\r\nrslt3 = list(gvn_list)\r\nprint(\"The result after applying list() function on the given list = \", rslt3)\r\n<\/pre>\n
The result after applying list() function on the given tuple = [1, 2, 3, 4, 5]\r\nThe result after applying list() function on the given string = ['h', 'e', 'l', 'l', 'o']\r\nThe result after applying list() function on the given list = [10, 20, 30, 40]<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Give the tuple as user input using tuple(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_tupl = tuple(map(int, input(\r\n 'Enter some random Tuple Elements separated by spaces = ').split()))\r\n# Give the string as user input using the input() function and \r\n# store it in another variable.\r\ngvn_str = input(\"Enter some random string = \")\r\n# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Pass the given tuple as an argument to the list() function that converts a given\r\n# tuple to list and returns a list.\r\n# Store it in another variable.\r\nrslt1 = list(gvn_tupl)\r\n# Print the result list after applying list() function on the given tuple.\r\nprint(\"The result after applying list() function on the given tuple = \", rslt1)\r\n# similarly do the same for the given string and list and print it.\r\nrslt2 = list(gvn_str)\r\nprint(\"The result after applying list() function on the given string = \", rslt2)\r\nrslt3 = list(gvn_lst)\r\nprint(\"The result after applying list() function on the given list = \", rslt3)\r\n<\/pre>\n
Enter some random Tuple Elements separated by spaces = 3 4 5 1\r\nEnter some random string = good\r\nEnter some random List Elements separated by spaces = 0 1 2 3\r\nThe result after applying list() function on the given tuple = [3, 4, 5, 1]\r\nThe result after applying list() function on the given string = ['g', 'o', 'o', 'd']\r\nThe result after applying list() function on the given list = [0, 1, 2, 3]<\/pre>\n","protected":false},"excerpt":{"rendered":"