
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python – Convert Suffix denomination to Values
When it is required to convert the suffix denomination to values, the dictionary is iterated over and the ‘replace’ method is used to convert them to values.
Example
Below is a demonstration of the same
my_list = ["5Cr", "7M", "9B", "12L", "20Tr", "30K"] print("The list is :") print(my_list) value_dict = {"M": 1000000, "B": 1000000000, "Cr": 10000000, "L": 100000, "K": 1000, "Tr": 1000000000000} my_result = [] for element in my_list: for key in value_dict: if key in element: val = float(element.replace(key, "")) * value_dict[key] my_result.append(val) print("The resultant dictionary values :") print(my_result)
Output
The list is : ['5Cr', '7M', '9B', '12L', '20Tr', '30K'] The resultant dictionary values : [50000000.0, 7000000.0, 9000000000.0, 1200000.0, 20000000000000.0, 30000.0]
Explanation
A list is defined and is displayed on the console.
Another dictionary is defined with certain denomination values.
An empty list is created.
The original list is iterated over, and the keys in the dictionary are iterated over.
If the key is present in the list, it is converted to float type, and multiplied with key of the dictionary.
This is assigned to a variable.
This variable is appended to the empty list.
This is the result which is displayed on the console.
- Related Articles
- Python - Selective consecutive Suffix Join
- How to convert Python dictionary keys/values to lowercase?
- Convert key-values list to flat dictionary in Python
- Python program to convert complex number to polar coordinate values
- How to Add Prefix or Suffix into Cell Values in Google Sheets?
- Python program to delete suffix from the given string
- Python Program to Group Strings by K length Using Suffix
- Suffix Array
- Check if a string is suffix of another in Python
- Appending suffix to numbers in JavaScript
- Program to count number of operations required to convert all values into same in Python?
- Python Pandas - Convert a MultiIndex to an Index of Tuples containing the level values
- MySQL query to convert empty values to NULL?
- Program to convert each element in row and column to zero for zero values in Python
- Python Check if suffix matches with any string in given list?

Advertisements