Table of Contents
Using for
Loop
To convert the entire list into one integer in Python:
- Create a list having integer-type elements.
- Declare and initialize a variable with
0
; it will be used to hold the final integer value. - Use the
for
loop to iterate over the list items. - In each iteration:
- multiply the variable created in the second step with
10
and add the current list item. - Assign the value (produced in the previous step) to the variable created in step two.
- multiply the variable created in the second step with
1 2 3 4 5 6 7 |
my_list = [5, 6, 7] number = 0 for current_digit in my_list: number = number*10 + current_digit print(number) |
1 2 3 |
567 |
First, we created a list named my_list
with three elements of integer type. Then, our job is to convert that from a list of integers to one integer value; for that, we defined and initialized the variable named number
with 0
. Then, we used the for
loop to go through all the list items.
For every current_digit
in the my_list
, we multiplied the number
with 10
and added current_digit
. This will have the effect of shifting digits of the number
variable to the left by one place and adding the upcoming digit to the right. For instance, we have a [5, 6, 7]
list in the above example, which will have the following iterations:
number = 0*10+5 = 5
number = 5*10+6 = 56
number = 56*10+7 = 567
Once the loop is over, we will have 567
as one integer type value; to check its type, you can use the type()
method as type(number)
.
Using List Comprehension
To convert the entire list into one integer in Python:
- Create a list having integer-type elements.
- Use list comprehension to convert each list item from integer type value to string type value; for instance, from
[5, 6, 7]
to['5', '6', '7']
. - Use the
join()
method to join all the list items to make a single string value, such as from['5', '6', '7']
to'567'
. - Use the
int()
method for changing data type from string to integer.
1 2 3 4 5 6 7 |
my_list = [5, 6, 7] new_list = [str(current_integer) for current_integer in my_list] string_value = "".join(new_list) number = int(string_value) print(number) |
1 2 3 |
567 |
After creating a list named my_list
having integer type elements, we used list comprehension to loop over all the list items and converted every item to a string by wrapping around with the str()
method. The str()
method converts the specified value to a string type value. At this point, we had my_list
as ['5', '6', '7']
, which we stored in the new_list
variable.
Next, we used the join()
method to join all the elements of new_list
to make it one string value and stored it in the string_value
variable. Using the join()
method, we transformed ['5', '6', '7']
to '567'
.
Now, we can use the int()
method to dynamically cast string_value
to an integer value which we saved in number
. Finally, we used the print()
method to print the value of the number
variable; we can also print the type of number
by printing as print(type(number))
.
Further reading:
Using map()
and join()
Methods
To convert the entire list into one integer in Python:
- Create a list having integer-type elements.
- Use the
map()
method to convert every list item’s type from integer to string. - Use the
join()
method to join all the list items to make it one string-type value. - Use the
int()
method to cast the type from string to integer dynamically.
1 2 3 4 5 6 7 |
my_list = [5, 6, 7] new_list = map(str, my_list) string_value = ''.join(new_list) number = int(string_value) print(number) |
1 2 3 |
567 |
This code fence is similar to the previous example, but we used functions instead of list comprehension. Here, we used the map() method to transform [5, 6, 7]
to ['5', '6', '7']
and saved it in new_list
variable. If you are curious how the map()
function works, then let’s understand that below.
We passed two arguments to map()
; the first was the function (str
), and the second was the iterable (my_list
). The map()
method iterated over the my_list
items where every list item was passed to the str
function as an argument; for instance, the first list item 5
was sent to the str
function as str(5)
, which returned string type value as '5'
. By using the map()
method, we transformed the [5, 6, 7]
to ['5', '6', '7']
, which we saved in the new_list
variable.
This new_list
variable was then passed to the join()
method to join all the list items and form one string type value as '567'
that we stored in string_value
. Finally, we performed typecasting using the int()
method. This int()
method took string_value
as a parameter and converted its type from string to integer that we saved in the number
variable.
Using the reduce()
Method with lambda
Expression
To convert the entire list into one integer in Python:
- Create a list having integer-type elements.
- Use the
reduce()
method with thelambda
function to convert the entire list to one integer.
1 2 3 4 5 6 |
from functools import reduce my_list = [5, 6, 7] number = reduce(lambda x, y: x*10 + y, my_list) print(number) |
1 2 3 |
567 |
This code example is the first example’s more concise and professional form. Here, we did the same, multiplied the number by 10
, added the current digit, and used this result for the next integer. But this time, number
is x
and current_digit
is y
, and we used the reduce()
function with the lambda
function to achieve this.
The reduce() method took two arguments: first was the function (lambda
), and second was the iterable (my_list
). The lambda
expression took two arguments, x
& y
, and produced x*10+y
. The reduce()
method’s job was to apply the lambda
function to the first two list items of my_list
, then apply the same function to the outcome and next item, and so on, till all items have been processed.
Once the reduce()
method is done with its processing, we will get the answer which would be an integer-type value.
That’s all about how to convert List to Integer in Python.