Lists, when created and by specifying values in a section of code, generate output similar to the following:
Code :
List
=
[
' GeeksForGeeks'
,
'VenD'
,
5
,
9.2
]
print
(
'List:'
,
List
)
Exit :
List: ['GeeksForGeeks',' VenD', 5, 9.2]
In the picture above, the defined list is a combination of integers and strings new values. The interpreter implicitly interprets "GeeksForGeeks" and "VenD" as string values, while 5 and 9.2 are interpreted as integer and floating point values, respectively. We can perform common arithmetic operations on integer and floating point values as follows.
Code :
# Common arithmetic operations on 5 and 9.2:
List
=
[
'GeeksForGeeks '
,
' VenD'
,
5
,
9.2
]
print
(
'List [2] +2, Answer:'
, end
=
'')
print
(
List
[
List
. index (
5
)] +
2
)
print
(
'List [3] +8.2, Answer:'
, end
=
'')
print
(
List
[
List
.index (
9.2
)]
+
8.2
)
Output :
List [2] +2, Answer: 7 List [3] +8.2, Answer: 17.4
In addition, stringspecific operations such as string concatenation can be performed on matching strings:
Code :
# String concatenation operation
# List: [& # 39; GeeksForGeeks & # 39 ;, & # 39; VenD & # 39 ;, 5, 9.2]
# Combining list [0] and list [1]
List
=
[
' GeeksForGeeks'
,
'VenD'
,
5
,
9.2
]
print
(
List
[
0
]
+
''
+
List
< code class = "plain"> [ 1
])
However, since we know that lists contain elements of different data types, which can be of type: string, integer, float comma, tuple, dictionaries or even a list of themselves (a list of lists), then this is not valid if you are generating a list as a result of user input. For example, consider the example below:
Code :
# All resulting List2 elements will be of string type
list2
=
[]
# This is a list that will contain elements as input from the user
element_count
=
int
(
input
(
'Enter Number of Elements you wish to enter:'
))
for
i
in
range
(element_count):
element
=
input
(f
'Enter Element {i + 1}:'
)
list2.append (element)
print
(
"List 2:"
, list2)
Exit :
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 the List2 generated by user input, i.e. Now only contains values of string data type. In addition, numeric elements have now lost the ability to undergo arithmetic operations, since they are of a string data type. This behavior directly contradicts the generic behavior of lists.
As programmers, we need to process user data and store it in an appropriate format for operations and manipulations on the target dataset to be effective.
In this approach, we will distinguish the data received from the user into three sections, namely: integer, string and floating point. To do this, we use a small code to perform the appropriate type casting operations.
Method to overcome the proposed limitation:
Code:

Exit :
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 method is essentially an algorithm that uses a regular expression library along with an algorithm to parse the data types of the inserted elements. After successfully analyzing the data structure, we proceed to perform the type conversion.
For example, consider the following cases:
Expression used for integer identification: r & # 39; / d + & # 39;
Output: This method, however, is only a small prototype of type values and processing the raw data before storing it in a list ... This definitely offers a fruitful result that overcomes the suggested limitations on list inputs. In addition, thanks to advanced regex applications and some algorithm improvements, it is possible to parse and store correspondingly more forms of data such as tuples, dictionaries, etc.
Benefits of Dynamic TypeCasting:
Limitations of dynamic TypeCasting:
HandsOn Machine Learning with ScikitLearn, Keras, and TensorFlow: Concepts, Tools, and Techniques to Build Intelligent Systems PDF, 2nd Edition. This book assumes you know next to nothing about m...
22/08/2021
Computer Age Statistical Inference: Algorithms, Evidence, and Data Science (Institute of Mathematical Statistics Monographs, Series Number 6). The twentyfirst century has seen a breathtaking expan...
28/08/2021
Python Crash Course is the world's bestselling guide to the Python programming language. This quick and indepth introduction to Python programming will get you started writing programs, solving prob...
11/08/2021
In the last decade, we have seen the impact of exponential advances in technology on the way we work, shop, communicate, and think. At the heart of this change is our ability to collect and gain insig...
10/07/2020