Lists in Python are linear containers used for storing data of various Data Types. The ability to store a variety of data is what makes Lists a very unique and vital Data Structure in Python.
Once created, lists can be modified further depending on one’s needs. Hence, they are ‘mutable.’
Lists, when created and by defining the values in the code-section, generates an output similar to this:
List: ['GeeksForGeeks', 'VenD', 5, 9.2]
In the above picture, the defined list is a combination of integer and string values. The interpreter implicitly interprets ‘GeeksForGeeks’ and ‘VenD’ as string values whereas 5 and 9.2 are interpreted as integer and float values respectively. We can perform the usual arithmetic operations on integer and float values as follows.
List+2, Answer: 7 List+8.2, Answer: 17.4
Also, the string-specific operations like string concatenation can be performed on the respective strings:
However, since we know that lists contain items of various data types those which might be of type: string, integer, float, tuple, dictionaries, or maybe even list themselves (a list of lists), the same is not valid if you are generating a list as a result of user input. For instance, consider the example below:
Enter Number of Elements you wish to enter: 4 Enter Element 1 : GeeksForGeeks Enter Element 2 : VenD Enter Element 3 : 5 Enter Element 4 : 9.2 List 2 : ['GeeksForGeeks', 'VenD', '5', '9.2']
You may notice that List2 generated as a result of User Input, now contains values of string data-type only. Also, the numerical elements have now lost the ability to undergo arithmetic operations since they are of string data-type. This behaviour straightaway contradicts the versatile behavior of Lists.
It becomes necessary for us as programmers to process the user data and store it in the appropriate format so that operations and manipulations on the target data set become efficient.
In this approach, we shall distinguish data obtained from the user into three sections, namely integer, string, and float. For this, we use a small code to carry out the respective typecasting operations.
A technique to Overcome the proposed Limitation:
Enter number of elements : 4 Enter Element 1: GeeksForGeeks Enter Element 2: VenD Enter Element 3: 5 Enter Element 4: 9.2 ['GeeksForGeeks', 'VenD', 5, 9.2]
The above technique is essentially an algorithm which uses the Regular Expression library along with an algorithm to analyze the data-types of elements being inserted. After successfully analyzing the pattern of data, we then proceed to perform the type conversion.
For example, consider the following cases:
- Case1: All values in the string are numeric. The user input is 7834, the Regular Expression function analyzes the given data and identifies that all values are digits between 0 to 9 hence the string ‘7834’ is typecasted to the equivalent integer value and then appended to the list as an integer.
Expression Used for Integer identification : r’\d+’
- Case2: The String expression contains elements which represent a floating point number. A floating point value is identified over a pattern of digits preceding or succeeding a full stop(‘.’). Ex: 567., .056, 6.7, etc.
Expression used for float value identification: r’\d*.\d*’
- Case3: String Input contains characters, special characters and numerical values as well. In this case, the data element is generalised as a string value. No special Regular Expression needed since the given expression will return false when being categorised as Integer or Float Values.
Ex: ‘155B, Baker Street ! ‘, ’12B72C_?’, ‘I am Agent 007’, ‘_GeeksForGeeks_’, etc.
Conclusion: This method, however, is just a small prototype of typecasting values and processing raw data before storing it in the list. It definitely offers a fruitful outcome which overcomes the proposed limitations on list inputs. Further, with the advanced applications of regular expressions and some improvements in the algorithm, more forms of data such as tuples, dictionaries, etc. can be analysed and stored accordingly.
Advantages of the Dynamic TypeCasting:
- Since data is stored in the appropriate format, various ‘type-specific’ operations can be performed on it. Ex: Concatenation in case of strings, Addition, Subtraction, Multiplication in case of numerical values and various other operations on the respective data types.
- Typecasting phase takes place while storing the data. Hence, the programmer does not have to worry about Type Errors which occur while performing operations on the data set.
Limitations of Dynamic TypeCasting:
- Some data elements which do not necessarily require typecasting undergo the process, resulting in unnecessary computing.
- Multiple condition-checks and function calls result in memory wastage when each time a new element is inserted.
- The flexibility of processing numerous types of data might require several new additions to the existing code as per the developer’s needs.
- Python | Program to count number of lists in a list of lists
- Python | Zipping two lists of lists
- Python | Intersection of two lists
- Python | All possible permutations of N lists
- Python | Union of Value Lists
- Python | Union of two or more Lists
- Python | Average of two lists
- Python | Dividing two lists
- Python | Merge key value lists
- Python | Difference between two lists
- Python | Using 2D arrays/lists the right way
- Python | Iterating two lists at once
- Python - Lists Modulo
- Python | Iterate through value lists dictionary
- Python | Integrity Sorting in two lists
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.