Python – Construct Grades List
Last Updated :
22 Apr, 2023
Given a number, construct a list having all the possible Grades combination for the first N characters.
Input : num = 3
Output : [‘A+’, ‘A’, ‘A-‘, ‘B+’, ‘B’, ‘B-‘, ‘C+’, ‘C’, ‘C-‘]
Explanation : All grades till C rendered in list.
Input : num = 5
Output : [‘A+’, ‘A’, ‘A-‘, ‘B+’, ‘B’, ‘B-‘, ‘C+’, ‘C’, ‘C-‘, ‘D+’, ‘D’, ‘D-‘, ‘E+’, ‘E’, ‘E-‘]
Explanation : 5 corresponds to E, hence all combinations.
Method #1 : Using list comprehension + ord()
The combination of the above functions can be used to solve this problem. In this, we perform the task of incrementing and extracting ASCII characters using ord() and list comprehension is used in character list creation.
Python3
num = 4
res = [ chr ( ord ( 'A' ) + idx) + sym for idx in range (num)
for sym in [ '+' , ' ', ' - ']]
print ( "Grades List : " + str (res))
|
Output :
Grades List : ['A+', 'A', 'A-', 'B+', 'B', 'B-', 'C+', 'C', 'C-', 'D+', 'D', 'D-']
Time complexity:O(n^2), where n is the value of “num”.
Auxiliary space: O(n^2), as well.
Method #2 : Using join() + map() + product() + ascii_uppercase
The combination of above functions can be used to solve this problem. In this, we perform the task of extracting ascii characters using ascii_uppercase, product() and map(), is used to perform linkage with symbols, the result is created after perform join of all.
Python3
from string import ascii_uppercase
from itertools import product
num = 4
res = [ * map (' '.join, product(ascii_uppercase[:num], [' + ', ' ', ' - ']))]
print ( "Grades List : " + str (res))
|
Output
Grades List : ['A+', 'A', 'A-', 'B+', 'B', 'B-', 'C+', 'C', 'C-', 'D+', 'D', 'D-']
Time Complexity: O(n2)
Auxiliary Space: O(n)
Method #3: Using loop and list methods
Python3
num = 3
x = [ "+" , " ", " - "]
alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
res = []
for i in range ( 0 , num):
b = []
for j in x:
s = alphabets[i] + j
b.append(s)
res.extend(b)
print ( "Grades List : " + str (res))
|
Output
Grades List : ['A+', 'A', 'A-', 'B+', 'B', 'B-', 'C+', 'C', 'C-']
Time Complexity: O(N^2), where N is the value of the variable num. The nested for loops iterate N times each.
Auxiliary Space: O(N), as we are creating a list of length N, which is the variable res.
Method #4: Using itertools.product()
Step-by-step approach:
Import the itertools module to access the product() function.
Define the range of values for the first letter of the grade as a string.
Define the range of values for the second letter of the grade as a list.
Use the product() function to create a Cartesian product of the two ranges.
Flatten the list of tuples obtained from the Cartesian product to create the final grades list.
Python3
import itertools
num = 3
x = [ "+" , " ", " - "]
alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
grades = list (itertools.product(alphabets[:num], x))
res = [''.join(grade) for grade in grades]
print ( "Grades List: " + str (res))
|
Output
Grades List: ['A+', 'A', 'A-', 'B+', 'B', 'B-', 'C+', 'C', 'C-']
Time complexity: O(NM), where N is the value of num and M is the length of the x list.
Auxiliary space: O(NM), as we are creating a list of tuples of size NxM and then flattening it to create the final list.
Similar Reads
Python - Create a List of Floats
Creating a list of floats in Python can be done in several simple ways. The easiest way to create a list of floats is to define the numbers directly. Hereâs how we can do it: [GFGTABS] Python a = [0.1, 2.3, 4.5, 6.7] print(a) [/GFGTABS]Output[0.1, 2.3, 4.5, 6.7] Using List ComprehensionList comprehe
2 min read
Python List Creation Programs
Python provides multiple ways to create lists based on different requirements, such as generating lists of numbers, creating nested lists, forming lists of tuples or dictionaries, and more. This article covers various ways to create lists efficiently, including: Generating lists of numbers, strings,
2 min read
Python - Values Frequency Index List
Sometimes, while working with Python tuples, we can have a problem in which we need to extract the frequency of each value in tuple. This has been solved earlier. We can have a modification in which we need to create list in which index represents the key and value of it represents the frequency of
4 min read
Python - Create List of Size n
Creating a list of size n in Python can be done in several ways. The easiest and most efficient way to create a list of size n is by multiplying a list. This method works well if we want to fill the list with a default value, like 0 or None. [GFGTABS] Python # Size of the list n = 5 # Creating a lis
2 min read
Program to create grade calculator in Python
Given different scored marks of students. We need to find a Grade Calculator in Python. The test score is an average of the respective marks scored in assignments, tests, and lab work. The final test score is assigned using the below formula. 10% of marks scored from submission of Assignments 70% of
4 min read
Python - Flatten Nested Keys
Sometimes, while working with Python data, we can have a problem in which we need to perform the flattening of certain keys in nested list records. This kind of problem occurs while data preprocessing. Let us discuss certain ways in which this task can be performed. Method #1: Using loopThis is a br
10 min read
Python - Conditional Prefix in List
Given a list of elements, attach different prefix according to condition. Input : test_list = [45, 53, 76, 86, 3, 49], pref_1 = "LOSE-", pref_2 = "WIN-" Output : ['LOSE-45', 'WIN-53', 'WIN-76', 'WIN-86', 'LOSE-3', 'LOSE-49'] Explanation : All 50+ are prefixed as "WIN-" and others as "LOSE-". Input :
8 min read
Create Dictionary from the List-Python
The task of creating a dictionary from a list in Python involves mapping each element to a uniquely generated key, enabling structured data storage and quick lookups. For example, given a = ["gfg", "is", "best"] and prefix k = "def_key_", the goal is to generate {'def_key_gfg': 'gfg', 'def_key_is':
3 min read
Output of Python programs | Set 8
Prerequisite - Lists in Python Predict the output of the following Python programs. Program 1 [GFGTABS] Python list = [1, 2, 3, None, (1, 2, 3, 4, 5), ['Geeks', 'for', 'Geeks']] print len(list) [/GFGTABS]Output: 6Explanation: The beauty of python list datatype is that within
3 min read
Extract Dictionary Values as a Python List
To extract dictionary values from a list, we iterate through each dictionary, check for the key's presence, and collect its value. The result is a list of values corresponding to the specified key across all dictionaries.For example, given data = {'a': 1, 'b': 2, 'c': 3}, the output will be [1, 2, 3
3 min read