2

I am getting input in the form of:

4 11111

I am using a,b = map(int,raw_input().split()) to store 4 in a and 11111 in b. But I want b to be a list, how am I supposed to do that ?

2
  • 3
    You want b to be a list of what? [11111], [1, 1, 1, 1, 1]? And what input are you expecting in general? Commented Apr 11, 2015 at 16:35
  • I want b to be [1, 1, 1, 1, 1] so that I can add all the values in b . Commented Apr 11, 2015 at 16:41

3 Answers 3

4

You can just use list :

>>> list('11111')
['1', '1', '1', '1', '1']

But in that case you can not use map function because it just apply one function on its iterable argument and in your code it convert the whole of '11111' to integer so you have tow way :

  1. create b as a list of string ones :
inp=raw_input().split()
a,b = int(inp[0]),list(inp[1])
  1. if you want a list of integer 1's use map :
>>> map(int,'11111')
[1, 1, 1, 1, 1]
Sign up to request clarification or add additional context in comments.

2 Comments

But I need the 1`s in list to be integers
Always provide links to the docs. docs.python.org/2/library/functions.html#map
3

You can try this in two steps:

a, b = raw_input().split()
a, b = int(a), map(int, b)
print a
print b

Returns: 4 and [1, 1, 1, 1, 1]

Comments

3
a, b = map(int, raw_input().strip().split())

char list:

l = list(str(b))

int list:

l = [int(i) for i in str(b)]

5 Comments

there is no need to strip, you also end up with strings so why cast to int and then turn them back into strings again?
fyi, ozgur added strip()
who added the l = list(str(b))?
I wrote everything except strip().
a, b = "4 11111".split();a, b = int(a), list(b) will be more efficient.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.