seriestoo2
seriestoo2

Reputation: 39

Python: How to add an integer to an empty list, without using a method?

I need to add an integer on to the end of a list. One constraint for the problem I'm solving is that I can't use any methods, so I can't use .append().

I've tried using

list = []
list += someInt

but that returns

TypeError: 'int' object is not iterable

Any help would be much appreciated!

Upvotes: 0

Views: 2920

Answers (2)

labheshr
labheshr

Reputation: 3056

One more way for some more flavor :) ...IF you can't just use append, but can use some other method (extend) in this case

def t2():
    lst = [1,2]
    lst.extend([3])
    print lst #prints [1, 2, 3]

Upvotes: -1

James
James

Reputation: 1238

How about lst = lst + [someInt].

Upvotes: 3

Related Questions