Convert List of string to Integer
I have a list whose elements are only single string.
my_list = ['9', '4', '4', '5', '4', '3', '5']
How can I convert this into a single integer like: 9445435
Note: ' '.join()
only works for lists of strings, not integers.
python python-3.x
add a comment |
I have a list whose elements are only single string.
my_list = ['9', '4', '4', '5', '4', '3', '5']
How can I convert this into a single integer like: 9445435
Note: ' '.join()
only works for lists of strings, not integers.
python python-3.x
3
Usejoin
and then cast it to int! like =>int(''.join(my_list))
– DarkSuniuM
Nov 20 '18 at 8:43
1
Is your list made of integers or of strings? If integers, thenint(''.join(str(v) for v in my_list))
, otherwise what DarkSuniuM said
– FernAndr
Nov 20 '18 at 8:45
1
You say it's a list of integers, but the list you have in your question contains strings, not integers.
– khelwood
Nov 20 '18 at 8:53
add a comment |
I have a list whose elements are only single string.
my_list = ['9', '4', '4', '5', '4', '3', '5']
How can I convert this into a single integer like: 9445435
Note: ' '.join()
only works for lists of strings, not integers.
python python-3.x
I have a list whose elements are only single string.
my_list = ['9', '4', '4', '5', '4', '3', '5']
How can I convert this into a single integer like: 9445435
Note: ' '.join()
only works for lists of strings, not integers.
python python-3.x
python python-3.x
edited Nov 20 '18 at 9:02
eyllanesc
79.4k103258
79.4k103258
asked Nov 20 '18 at 8:42
Oliver MurfettOliver Murfett
175
175
3
Usejoin
and then cast it to int! like =>int(''.join(my_list))
– DarkSuniuM
Nov 20 '18 at 8:43
1
Is your list made of integers or of strings? If integers, thenint(''.join(str(v) for v in my_list))
, otherwise what DarkSuniuM said
– FernAndr
Nov 20 '18 at 8:45
1
You say it's a list of integers, but the list you have in your question contains strings, not integers.
– khelwood
Nov 20 '18 at 8:53
add a comment |
3
Usejoin
and then cast it to int! like =>int(''.join(my_list))
– DarkSuniuM
Nov 20 '18 at 8:43
1
Is your list made of integers or of strings? If integers, thenint(''.join(str(v) for v in my_list))
, otherwise what DarkSuniuM said
– FernAndr
Nov 20 '18 at 8:45
1
You say it's a list of integers, but the list you have in your question contains strings, not integers.
– khelwood
Nov 20 '18 at 8:53
3
3
Use
join
and then cast it to int! like => int(''.join(my_list))
– DarkSuniuM
Nov 20 '18 at 8:43
Use
join
and then cast it to int! like => int(''.join(my_list))
– DarkSuniuM
Nov 20 '18 at 8:43
1
1
Is your list made of integers or of strings? If integers, then
int(''.join(str(v) for v in my_list))
, otherwise what DarkSuniuM said– FernAndr
Nov 20 '18 at 8:45
Is your list made of integers or of strings? If integers, then
int(''.join(str(v) for v in my_list))
, otherwise what DarkSuniuM said– FernAndr
Nov 20 '18 at 8:45
1
1
You say it's a list of integers, but the list you have in your question contains strings, not integers.
– khelwood
Nov 20 '18 at 8:53
You say it's a list of integers, but the list you have in your question contains strings, not integers.
– khelwood
Nov 20 '18 at 8:53
add a comment |
3 Answers
3
active
oldest
votes
Try this,
In [5]: int(''.join(my_list))
Out[5]: 9445435
add a comment |
Why don't you try this approach?
my_list = ['9', '4', '4', '5', '4', '3', '5']
output = int(''.join(str(num) for num in my_list))
print(output) ## 9445435
1
oroutput = int(''.join(my_list))
– eyllanesc
Nov 20 '18 at 8:51
@eyllanesc nah, thefor
is there so if the list items was int, it casts them to str
– DarkSuniuM
Nov 20 '18 at 8:53
1
@DarkSuniuMjoin
accepts an iterable and converts them internally withstr()
to each iterable element, thefor
is unnecessary
– eyllanesc
Nov 20 '18 at 8:55
@eyllanescTypeError: sequence item 0: expected str instance, int found
– DarkSuniuM
Nov 20 '18 at 8:56
@DarkSuniuMmy_list = ['9', '4', '4', '5', '4', '3', '5'] output = int(''.join(my_list)) print(output)
---> 9445435 in Python 2.7.15 and python 3.7.1
– eyllanesc
Nov 20 '18 at 8:58
|
show 5 more comments
result = ' '.join(my_list)
result = int(result)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53389136%2fconvert-list-of-string-to-integer%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try this,
In [5]: int(''.join(my_list))
Out[5]: 9445435
add a comment |
Try this,
In [5]: int(''.join(my_list))
Out[5]: 9445435
add a comment |
Try this,
In [5]: int(''.join(my_list))
Out[5]: 9445435
Try this,
In [5]: int(''.join(my_list))
Out[5]: 9445435
answered Nov 20 '18 at 8:49
Rahul K PRahul K P
7,37122234
7,37122234
add a comment |
add a comment |
Why don't you try this approach?
my_list = ['9', '4', '4', '5', '4', '3', '5']
output = int(''.join(str(num) for num in my_list))
print(output) ## 9445435
1
oroutput = int(''.join(my_list))
– eyllanesc
Nov 20 '18 at 8:51
@eyllanesc nah, thefor
is there so if the list items was int, it casts them to str
– DarkSuniuM
Nov 20 '18 at 8:53
1
@DarkSuniuMjoin
accepts an iterable and converts them internally withstr()
to each iterable element, thefor
is unnecessary
– eyllanesc
Nov 20 '18 at 8:55
@eyllanescTypeError: sequence item 0: expected str instance, int found
– DarkSuniuM
Nov 20 '18 at 8:56
@DarkSuniuMmy_list = ['9', '4', '4', '5', '4', '3', '5'] output = int(''.join(my_list)) print(output)
---> 9445435 in Python 2.7.15 and python 3.7.1
– eyllanesc
Nov 20 '18 at 8:58
|
show 5 more comments
Why don't you try this approach?
my_list = ['9', '4', '4', '5', '4', '3', '5']
output = int(''.join(str(num) for num in my_list))
print(output) ## 9445435
1
oroutput = int(''.join(my_list))
– eyllanesc
Nov 20 '18 at 8:51
@eyllanesc nah, thefor
is there so if the list items was int, it casts them to str
– DarkSuniuM
Nov 20 '18 at 8:53
1
@DarkSuniuMjoin
accepts an iterable and converts them internally withstr()
to each iterable element, thefor
is unnecessary
– eyllanesc
Nov 20 '18 at 8:55
@eyllanescTypeError: sequence item 0: expected str instance, int found
– DarkSuniuM
Nov 20 '18 at 8:56
@DarkSuniuMmy_list = ['9', '4', '4', '5', '4', '3', '5'] output = int(''.join(my_list)) print(output)
---> 9445435 in Python 2.7.15 and python 3.7.1
– eyllanesc
Nov 20 '18 at 8:58
|
show 5 more comments
Why don't you try this approach?
my_list = ['9', '4', '4', '5', '4', '3', '5']
output = int(''.join(str(num) for num in my_list))
print(output) ## 9445435
Why don't you try this approach?
my_list = ['9', '4', '4', '5', '4', '3', '5']
output = int(''.join(str(num) for num in my_list))
print(output) ## 9445435
answered Nov 20 '18 at 8:50
toom501toom501
3021313
3021313
1
oroutput = int(''.join(my_list))
– eyllanesc
Nov 20 '18 at 8:51
@eyllanesc nah, thefor
is there so if the list items was int, it casts them to str
– DarkSuniuM
Nov 20 '18 at 8:53
1
@DarkSuniuMjoin
accepts an iterable and converts them internally withstr()
to each iterable element, thefor
is unnecessary
– eyllanesc
Nov 20 '18 at 8:55
@eyllanescTypeError: sequence item 0: expected str instance, int found
– DarkSuniuM
Nov 20 '18 at 8:56
@DarkSuniuMmy_list = ['9', '4', '4', '5', '4', '3', '5'] output = int(''.join(my_list)) print(output)
---> 9445435 in Python 2.7.15 and python 3.7.1
– eyllanesc
Nov 20 '18 at 8:58
|
show 5 more comments
1
oroutput = int(''.join(my_list))
– eyllanesc
Nov 20 '18 at 8:51
@eyllanesc nah, thefor
is there so if the list items was int, it casts them to str
– DarkSuniuM
Nov 20 '18 at 8:53
1
@DarkSuniuMjoin
accepts an iterable and converts them internally withstr()
to each iterable element, thefor
is unnecessary
– eyllanesc
Nov 20 '18 at 8:55
@eyllanescTypeError: sequence item 0: expected str instance, int found
– DarkSuniuM
Nov 20 '18 at 8:56
@DarkSuniuMmy_list = ['9', '4', '4', '5', '4', '3', '5'] output = int(''.join(my_list)) print(output)
---> 9445435 in Python 2.7.15 and python 3.7.1
– eyllanesc
Nov 20 '18 at 8:58
1
1
or
output = int(''.join(my_list))
– eyllanesc
Nov 20 '18 at 8:51
or
output = int(''.join(my_list))
– eyllanesc
Nov 20 '18 at 8:51
@eyllanesc nah, the
for
is there so if the list items was int, it casts them to str– DarkSuniuM
Nov 20 '18 at 8:53
@eyllanesc nah, the
for
is there so if the list items was int, it casts them to str– DarkSuniuM
Nov 20 '18 at 8:53
1
1
@DarkSuniuM
join
accepts an iterable and converts them internally with str()
to each iterable element, the for
is unnecessary– eyllanesc
Nov 20 '18 at 8:55
@DarkSuniuM
join
accepts an iterable and converts them internally with str()
to each iterable element, the for
is unnecessary– eyllanesc
Nov 20 '18 at 8:55
@eyllanesc
TypeError: sequence item 0: expected str instance, int found
– DarkSuniuM
Nov 20 '18 at 8:56
@eyllanesc
TypeError: sequence item 0: expected str instance, int found
– DarkSuniuM
Nov 20 '18 at 8:56
@DarkSuniuM
my_list = ['9', '4', '4', '5', '4', '3', '5'] output = int(''.join(my_list)) print(output)
---> 9445435 in Python 2.7.15 and python 3.7.1– eyllanesc
Nov 20 '18 at 8:58
@DarkSuniuM
my_list = ['9', '4', '4', '5', '4', '3', '5'] output = int(''.join(my_list)) print(output)
---> 9445435 in Python 2.7.15 and python 3.7.1– eyllanesc
Nov 20 '18 at 8:58
|
show 5 more comments
result = ' '.join(my_list)
result = int(result)
add a comment |
result = ' '.join(my_list)
result = int(result)
add a comment |
result = ' '.join(my_list)
result = int(result)
result = ' '.join(my_list)
result = int(result)
edited Nov 20 '18 at 8:52
AkshayNevrekar
4,83791837
4,83791837
answered Nov 20 '18 at 8:48
crazy_codercrazy_coder
14116
14116
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53389136%2fconvert-list-of-string-to-integer%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
Use
join
and then cast it to int! like =>int(''.join(my_list))
– DarkSuniuM
Nov 20 '18 at 8:43
1
Is your list made of integers or of strings? If integers, then
int(''.join(str(v) for v in my_list))
, otherwise what DarkSuniuM said– FernAndr
Nov 20 '18 at 8:45
1
You say it's a list of integers, but the list you have in your question contains strings, not integers.
– khelwood
Nov 20 '18 at 8:53