python - How to append a list with a single string to a list? -
i wondering how able append list list?
x = [] x.append(list(('h4','h3'))) print x # [['h4', 'h3']] x.append(list('h4')) print x # [['h4', 'h3'], ['h','4']]
i wondering how [['h4', 'h3'], ['h4']]
instead of [['h4', 'h3'], ['h','4']]
. scoured web , saw x.extend
isn't wanted :\
you can use []
instead of list
:
x.append(['h4'])
the list
function (which constructs new list) takes iterable in parameter , adds every element of iterable in list. but, strings iterable in python, each element (characters here) added elements in list. using []
shortcut avoid that.
list([iterable])
return list items same , in same order iterable‘s items. iterable may either sequence, container supports iteration, or iterator object. if iterable list, copy made , returned, similar iterable[:]. for instance,
list('abc')
returns['a', 'b', 'c']
, list( (1, 2, 3) ) returns [1, 2, 3]. if no argument given, returns new empty list, [].
Comments
Post a Comment