the input is [120, [10, 20, 30]]
i split the balance using this code

def checkio(data):
    balance=[int(x) for x in data[0:1]]
    withdrawal=data[1:2]

but when i split it the withdrawal section looks like [[10,20,30]]
which hinders the loops im going to do.
I want it to have single brackets like so: [10,20,30]
how do i go about doing this

Recommended Answers

All 5 Replies

data[1:2] is a list splice. It returns the sublist of data that goes from index 1 (inclusive) to index 2 (exclusive). So basically it returns a list that contains the element at index 1 and nothing else. If all you want is the element at index 1 without a list wrapped around it, just use normal indexing:

withdrawal = data[1]

It also seems strange to me that you use a list comprehension on a 1-element list to get the balance. I would just do balance = int(data[0]) if I were you. If there's a good reason why balance should be a 1-element list, you could also do balance = [ int(data[0]) ], but I don't see what that reason could be.

The list show only integer,so why convert at all.

>>> lst = [120, [10, 20, 30]]
>>> lst[0]
120
>>> lst[1]
[10, 20, 30]
>>> type(lst[0])
<type 'int'>

>>> balance, withdrawal = lst[0], lst[1]
>>> balance
120
>>> withdrawal
[10, 20, 30]

data[1:2][0]

Why would one possibly write that rather than data[1]?

Thanks everyone!
It was so simple i cant beleive i overlooked it!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.