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:
Sort List Elements by Frequency - Python
Next article icon

Python – Sort by Uppercase Frequency

Last Updated : 08 Mar, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a list of strings, perform sorting by frequency of uppercase characters. 

Input : test_list = [“Gfg”, “is”, “FoR”, “GEEKS”] 
Output : [‘is’, ‘Gfg’, ‘FoR’, ‘GEEKS’] 
Explanation : 0, 1, 2, 5 uppercase letters in strings respectively.
 

Input : test_list = [“is”, “GEEKS”] 
Output : [‘is’, ‘GEEKS’] 
Explanation : 0, 5 uppercase letters in strings respectively. 

Method #1 : Using sort() + isupper()

In this, we perform task of checking for uppercase using isupper(), and sort() to perform task of sorting.

Python3




# Python3 code to demonstrate working of
# Sort by Uppercase Frequency
# Using isupper() + sort()
 
 
# helper function
def upper_sort(sub):
 
    # len() to get total uppercase characters
    return len([ele for ele in sub if ele.isupper()])
 
 
# initializing list
test_list = ["Gfg", "is", "BEST", "FoR", "GEEKS"]
 
# printing original list
print("The original list is: " + str(test_list))
 
# using external function to perform sorting
test_list.sort(key=upper_sort)
 
# printing result
print("Elements after uppercase sorting: " + str(test_list))
 
 
Output
The original list is: ['Gfg', 'is', 'BEST', 'FoR', 'GEEKS'] Elements after uppercase sorting: ['is', 'Gfg', 'FoR', 'BEST', 'GEEKS']

Time Complexity: O(n*nlogn), where n is the length of the input list. This is because we’re using the built-in sorted() function which has a time complexity of O(nlogn) in the worst case and isupper has a time complexity of O(n) in the worst case.
Auxiliary Space: O(1), as we’re not using any additional space other than the input list itself. 

Method #2 : Using sorted() + lambda function

In this, we perform the task of sorting using sorted(), and lambda function is used rather than external sort() function to perform task of sorting.

Python3




# Python3 code to demonstrate working of
# Sort by Uppercase Frequency
# Using sorted() + lambda function
 
 
# initializing list
test_list = ["Gfg", "is", "BEST", "FoR", "GEEKS"]
 
# printing original list
print("The original list is: " + str(test_list))
 
# sorted() + lambda function used to solve problem
res = sorted(test_list, key=lambda sub: len(
    [ele for ele in sub if ele.isupper()]))
 
# printing result
print("Elements after uppercase sorting: " + str(res))
 
 
Output
The original list is: ['Gfg', 'is', 'BEST', 'FoR', 'GEEKS'] Elements after uppercase sorting: ['is', 'Gfg', 'FoR', 'BEST', 'GEEKS']

Method #3 : Using Counter

This approach uses the Counter method from the collections module to get the frequency count of uppercase letters in each string and then uses the sorted method to sort the list based on these counts.

Python3




# Method #3 : Using Counter
 
# Python3 code to demonstrate working of
# Sort by Uppercase Frequency
# Using Counter
 
from collections import Counter
 
# initializing list
test_list = ["Gfg", "is", "BEST", "FoR", "GEEKS"]
 
# printing original list
print("The original list is: " + str(test_list))
 
# Using Counter to get uppercase frequency count for each string
uppercase_counts = [Counter(string)['A']+Counter(string)['B']+Counter(string)['C']+Counter(string)['D']+Counter(string)['E']+Counter(string)['F']+Counter(string)['G']+Counter(string)['H']+Counter(string)['I']+Counter(string)['J']+Counter(string)['K']+Counter(string)['L']+Counter(string)['M']+Counter(string)['N']+Counter(string)['O']+Counter(string)['P']+Counter(string)['Q']+Counter(string)['R']+Counter(string)['S']+Counter(string)['T']+Counter(string)['U']+Counter(string)['V']+Counter(string)['W']+Counter(string)['X']+Counter(string)['Y']+Counter(string)['Z'] for string in test_list]
 
# Using zip and sorted to sort the list based on uppercase frequency count
res = [x for _, x in sorted(zip(uppercase_counts, test_list))]
 
# printing result
print("Elements after uppercase sorting: " + str(res))
 
 
Output
The original list is: ['Gfg', 'is', 'BEST', 'FoR', 'GEEKS'] Elements after uppercase sorting: ['is', 'Gfg', 'FoR', 'BEST', 'GEEKS']

Time Complexity: O(n^2), as for each string in the list, we are checking the frequency of uppercase letters in that string using the Counter method which takes O(n) time, and we are doing it for n strings. So, the total time complexity will be O(n^2).

Auxiliary Space: O(n), as we are using a list of size n to store the uppercase frequency count for each string.

Method#4: using re module

Step-by-step algorithm:

  1. Define a list of strings to be sorted.
  2. Define a function uppercase_frequency() that takes a string as input and returns the frequency of uppercase characters in the string. This is done using the findall() function from the re module to find all uppercase letters in the string, and then using the len() function to count the number of matches.
  3. Use the sorted() function to sort the list of strings based on the result of the uppercase_frequency() function. The key parameter is set to the uppercase_frequency() function to indicate that the sorting should be done based on the result of this function for each string.
  4. Print the original list and the sorted list.

Python3




import re
 
# Define a list of strings to be sorted
test_list = ["Gfg", "is", "BEST", "FoR", "GEEKS"]
 
# Define a function to calculate the frequency of uppercase characters in a string
def uppercase_frequency(s):
    return len(re.findall(r'[A-Z]', s))
 
# Use the sorted() function to sort the list of strings by their uppercase frequency
sorted_list = sorted(test_list, key=uppercase_frequency)
 
# Print the original list and the sorted list
print("The original list is: " + str(test_list))
print("Elements after uppercase sorting: " + str(sorted_list))
 
 
Output
The original list is: ['Gfg', 'is', 'BEST', 'FoR', 'GEEKS'] Elements after uppercase sorting: ['is', 'Gfg', 'FoR', 'BEST', 'GEEKS']

Time complexity:
The time complexity of the uppercase_frequency() function is O(n), where n is the length of the input string, since it uses the findall() function to search the entire string for uppercase letters. The time complexity of the sorted() function is O(n log n), where n is the length of the list, since it performs a comparison-based sort. Therefore, the overall time complexity of the code is O(n log n).

Auxiliary space:
The auxiliary space complexity of the uppercase_frequency() function is O(n), where n is the length of the input string, since it creates a list of all uppercase letters in the string. The sorted() function uses O(log n) space for the recursive calls in the sorting algorithm, but this is negligible compared to the space used by the input list and the uppercase_frequency() function. Therefore, the overall auxiliary space complexity of the code is O(n).



Next Article
Sort List Elements by Frequency - Python
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python list-programs
  • Python string-programs
Practice Tags :
  • python

Similar Reads

  • Python - Assign Frequency to Tuples
    Given tuple list, assign frequency to each tuple in list. Input : test_list = [(6, 5, 8), (2, 7), (6, 5, 8), (9, ), (2, 7)] Output : [(6, 5, 8, 2), (2, 7, 2), (9, 1)] Explanation : (2, 7) occurs 2 times, hence 2 is append in tuple.Input : test_list = [(2, 7), (2, 7), (6, 5, 8), (9, ), (2, 7)] Output
    8 min read
  • Python - Sort rows by Frequency of K
    Given a Matrix, the task is to write a Python program to perform sorting on rows depending on the frequency of K. Input : test_list = [[10, 2, 3, 2, 3], [5, 5, 4, 7, 7, 4], [1, 2], [1, 1, 2, 2, 2]], K = 2 Output : [[5, 5, 4, 7, 7, 4], [1, 2], [10, 2, 3, 2, 3], [1, 1, 2, 2, 2]] Explanation : 0 < 1
    4 min read
  • Python - Every Kth Strings Uppercase
    Given a String list, change every Kth string to uppercase. Input : test_list = ["gfg", "is", "best", "for", "geeks"], K = 3 Output : ['GFG', 'is', 'best', 'FOR', 'geeks'] Explanation : All Kth strings are uppercased. Input : test_list = ["gfg", "is", "best", "for", "geeks"], K = 4 Output : ['GFG', '
    4 min read
  • Python - Uppercase Half String
    The problem is to convert half of a string to uppercase, either the first half or the second half, depending on the requirement. For example, given the string "python", the output could be "PYThon" (uppercase first half) or "pytHON" (uppercase second half). If the string length is odd, handle the mi
    2 min read
  • Sort List Elements by Frequency - Python
    Our task is to sort the list based on the frequency of each element. In this sorting process, elements that appear more frequently will be placed before those with lower frequency. For example, if we have: a = ["Aryan", "Harsh", "Aryan", "Kunal", "Harsh", "Aryan"] then the output should be: ['Aryan'
    3 min read
  • Python - Bigrams Frequency in String
    Sometimes while working with Python Data, we can have problem in which we need to extract bigrams from string. This has application in NLP domains. But sometimes, we need to compute the frequency of unique bigram for data collection. The solution to this problem can be useful. Lets discuss certain w
    4 min read
  • Python - Sort Strings by maximum frequency character
    Given a string, the task is to write a Python program to perform sort by maximum occurring character. Input : test_list = ["geekforgeeks", "bettered", "for", "geeks"] Output : ['for', 'geeks', 'bettered', 'geekforgeeks'] Explanation : 1 < 2 < 3 < 4, is ordering of maximum character occurren
    3 min read
  • Python - Sort Strings by Case difference
    Given Strings List, the task is to write a Python program to perform sort on basis of difference of cases i.e count of lower case and upper case. Examples: Input : test_list = ["GFG", "GeeKs", "best", "FOr", "alL", "GEEKS"] Output : ['GeeKs', 'FOr', 'alL', 'GFG', 'best', 'GEEKS'] Explanation : ees(3
    6 min read
  • Python - Successive Characters Frequency
    Sometimes, while working with Python strings, we can have a problem in which we need to find the frequency of next character of a particular word in string. This is quite unique problem and has the potential for application in day-day programming and web development. Let's discuss certain ways in wh
    6 min read
  • Python - All substrings Frequency in String
    Given a String, extract all unique substrings with their frequency. Input : test_str = "ababa" Output : {'a': 3, 'ab': 2, 'aba': 2, 'abab': 1, 'ababa': 1, 'b': 2, 'ba': 2, 'bab': 1, 'baba': 1} Explanation : All substrings with their frequency extracted. Input : test_str = "GFGF" Output : {'G': 2, 'G
    5 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