Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python | Convert String ranges to list
Next article icon

Python - Extract range sized strings

Last Updated : 13 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Sometimes, while working with huge amount of data, we can have a problem in which we need to extract just specific range sized strings. This kind of problem can occur during validation cases across many domains. Let’s discuss certain ways to handle this in Python strings list.

Method #1 : Using list comprehension + len() 
The combination of above functionalities can be used to perform this task. In this, we iterate for all the strings and return only ranged sized strings checked using len().

Python3
# Python3 code to demonstrate working of # Range length Strings extraction # using list comprehension + len()  # initialize list  test_list = ['gfg', 'is', 'best', 'for', 'geeks']  # printing original list  print("The original list : " + str(test_list))  # initialize i, j  i, j = 2, 3  # Range length Strings extraction # using list comprehension + len() res = [ele for ele in test_list if len(ele) >= i and len(ele) <= j]  # printing result print("The range sized strings are : " + str(res)) 

Output : 
The original list : ['gfg', 'is', 'best', 'for', 'geeks'] The range sized strings are : ['gfg', 'is', 'for']

 

Time Complexity: O(n) where n is the number of elements in the string list. The list comprehension + len() is used to perform the task and it takes O(n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list.

Method #2 : Using filter() + lambda 

The combination of above functionalities can be used to perform this task. In this, we extract the elements using filter() and logic is compiled in a lambda function.

Python3
# Python3 code to demonstrate working of # Range length Strings extraction # using filter() + lambda  # initialize list  test_list = ['gfg', 'is', 'best', 'for', 'geeks']  # printing original list  print("The original list : " + str(test_list))  # initialize i, j i, j = 2, 3  # Range length Strings extraction # using filter() + lambda res = list(filter(lambda ele: len(ele) >= i and len(ele) <= j, test_list))  # printing result print("The range sized strings are : " + str(res)) 

Output : 
The original list : ['gfg', 'is', 'best', 'for', 'geeks'] The range sized strings are : ['gfg', 'is', 'for']

 

Time Complexity: O(n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.

Method #3: Using a for loop and if statement

  • Initialize an empty list to store the range-sized strings.
  • Iterate over each string in the input list using a for loop.
  • Check if the length of the current string is greater than or equal to i and less than or equal to j using an if statement.
  • If the condition is true, append the current string to the result list.
  • Return the result list.
Python3
# Python3 code to demonstrate working of # Range length Strings extraction # using for loop and if statement  # initialize list  test_list = ['gfg', 'is', 'best', 'for', 'geeks']  # printing original list  print("The original list : " + str(test_list))  # initialize i, j i, j = 2, 3  # Range length Strings extraction # using for loop and if statement res = [] for ele in test_list:     if len(ele) >= i and len(ele) <= j:         res.append(ele)  # printing result print("The range sized strings are : " + str(res)) 

Output
The original list : ['gfg', 'is', 'best', 'for', 'geeks'] The range sized strings are : ['gfg', 'is', 'for']

Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(m), where m is the number of range sized strings found in the input list.


Next Article
Python | Convert String ranges to list
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python list-programs
Practice Tags :
  • python

Similar Reads

  • Python | Extract K sized strings
    Sometimes, while working with huge amount of data, we can have a problem in which we need to extract just specific sized strings. This kind of problem can occur during validation cases across many domains. Let's discuss certain ways to handle this in Python strings list. Method #1 : Using list compr
    5 min read
  • Python Extract Substring Using Regex
    Python provides a powerful and flexible module called re for working with regular expressions. Regular expressions (regex) are a sequence of characters that define a search pattern, and they can be incredibly useful for extracting substrings from strings. In this article, we'll explore four simple a
    2 min read
  • Python - Extract range characters from String
    Given a String, extract characters only which lie between given letters. Input : test_str = 'geekforgeeks is best', strt, end = "g", "s" Output : gkorgksiss Explanation : All characters after g and before s are retained. Input : test_str = 'geekforgeeks is best', strt, end = "g", "r" Output : gkorgk
    4 min read
  • Python - Extract string between two substrings
    The problem is to extract the portion of a string that lies between two specified substrings. For example, in the string "Hello [World]!", if the substrings are "[" and "]", the goal is to extract "World". If the starting or ending substring is missing, handle the case appropriately (e.g., return an
    3 min read
  • Python | Convert String ranges to list
    Sometimes, while working in applications we can have a problem in which we are given a naive string that provides ranges separated by a hyphen and other numbers separated by commas. This problem can occur across many places. Let's discuss certain ways in which this problem can be solved. Method #1:
    6 min read
  • Python - Extract K length substrings
    The task is to extract all possible substrings of a specific length, k. This problem involves identifying and retrieving those substrings in an efficient way. Let's explore various methods to extract substrings of length k from a given string in Python Using List Comprehension List comprehension is
    3 min read
  • Size of String in Memory - Python
    There can be situations in which we require to get the size that the string takes in bytes using Python which is usually useful in case one is working with files. Let's discuss certain ways in which this can be performed. Using sys.getsizeof()This task can also be performed by one of the system call
    2 min read
  • Python | Rear stray character String split
    C/C++ Code # Python3 code to demonstrate working of # Rear stray character String split # Using list comprehension # initializing string test_str = 'gfg, is, best, ' # printing original string print("The original string is : " + test_str) # Rear stray character String split # Using list co
    5 min read
  • Python - Filter Strings within ASCII range
    Given ASCII or alphabetical range, filter strings are found in a particular range. Input : test_list = ["gfg", "is", "best", "for", "geeks"], strt_asc, end_asc = 105, 115 Output : ['is'] Explanation : i has 105, and s has 115, which is in range ASCII values.Input : test_list = ["gfg", "is", "best",
    3 min read
  • Python | Reverse Interval Slicing String
    Sometimes, while working with strings, we can have a problem in which we need to perform string slicing. In this, we can have a variant in which we need to perform reverse slicing that too interval. This kind of application can come in day-day programming. Let us discuss certain ways in which this t
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences