{"id":17199,"date":"2021-08-26T10:43:03","date_gmt":"2021-08-26T05:13:03","guid":{"rendered":"https:\/\/python-programs.com\/?p=17199"},"modified":"2021-11-22T18:37:10","modified_gmt":"2021-11-22T13:07:10","slug":"python-program-to-print-items-from-a-list-with-specific-length","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-print-items-from-a-list-with-specific-length\/","title":{"rendered":"Python Program to Print Items from a List with Specific Length"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Add Number to each Element in a List<\/a> len() function<\/strong> :<\/p>\n The len() function is a Python built-in function.<\/p>\n The length of a string is returned as an integer value by the len() function.<\/p>\n The length of a string can be obtained by passing a string value as a parameter to the len() function.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example 2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to print items from a List with a given Specific Length.<\/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 Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n In the previous article, we have discussed Python Program to Add Number to each Element in a List Given a list of a string and some specific length and the task is to print items from a List with a given Specific Length. len() function : The len() function is a Python built-in function. The …<\/p>\n
\nGiven a list of a string and some specific length and the task is to print items from a List with a given Specific Length.<\/p>\nGiven List = ['hello', 'btechgeeks', 'good', 'morning']\r\nGiven length = 5<\/pre>\n
The Items from a given List with given Specific Length : \r\nhello<\/pre>\n
Given List = ['abcd', 'efghigdh', 'kjfatr', 'ihg', 'dfth']\r\nGiven length = 4<\/pre>\n
The Items from a given List with given Specific Length : \r\nabcd\r\ndfth<\/pre>\n
Program to Print Items from a List with Specific Length<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Give the List of String as static input and store it in a variable.\r\ngvn_lst = ['hello', 'btechgeeks', 'good', 'morning']\r\n# Given the length as static input and store it in another variable.\r\ngvn_len = 5\r\n# Loop in the above-given list using for loop.\r\nprint(\"The Items from a given List with given Specific Length : \")\r\nfor itr in gvn_lst:\r\n # Inside the loop, check if the length of the iterator is equal to the given length\r\n # using len() function and if conditional statement.\r\n if(len(itr)) == gvn_len:\r\n # If the statement is true, then print the iterator value(i.e. string).\r\n print(itr)\r\n<\/pre>\n
The Items from a given List with given Specific Length : \r\nhello<\/pre>\n
Method #2: Using For Loop (User Input)<\/h3>\n
\n
# Give the List of String as user input using list(),map(),input(),and \r\n#split() functions and store it in a variable.\r\ngvn_lst = list(map(str, input( 'Enter some random List Elements separated by spaces = ').split()))\r\n# Give the length as user input using int(input())and store it in another variable.\r\ngvn_len = int(input(\"Enter some random number = \"))\r\n# Loop in the above-given list using for loop.\r\nprint(\"The Items from a given List with given Specific Length : \")\r\nfor itr in gvn_lst:\r\n # Inside the loop, check if the length of the iterator is equal to the given length\r\n # using len() function and if conditional statement.\r\n if(len(itr)) == gvn_len:\r\n # If the statement is true, then print the iterator value(i.e. string).\r\n print(itr)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = abcd efghigdh kjfatr ihg dfth\r\nEnter some random number = 4\r\nThe Items from a given List with given Specific Length : \r\nabcd\r\ndfth<\/pre>\n
\n