Python Forum

Full Version: adding lists to lists?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hey everyone, i'm new in coding and i really need to create logic to help me in my job..
i am having trouble with creating a way to put lists into a existing one.. for example, i would like to make a input generate a list..

ex:
itens = []
itens.append(A)

i want this letter A to be placed into the list itens like this:

[[a]]

if i want to append a new letter it would generate a new list..

[[A], [B]]

is this even possible? it would be less complex to achieve what i need this way..
itens.append('A')
without the quotes, it's not a string
(Apr-09-2019, 08:43 PM)ivinjjunior Wrote: [ -> ]hey everyone, i'm new in coding and i really need to create logic to help me in my job..
i am having trouble with creating a way to put lists into a existing one.. for example, i would like to make a input generate a list..

ex:
itens = []
itens.append(A)

i want this letter A to be placed into the list itens like this:

[[a]]

if i want to append a new letter it would generate a new list..

[[A], [B]]

is this even possible? it would be less complex to achieve what i need this way..

itens = []
add_itens = input("append me: ")
itens.append([add_itens])
print(itens)
Like that?
A = 'a'
B = 'b'
items = []
items.append([A])
print(items)
items.append([B])
print(items)
Output:
[['a']] [['a'], ['b']]
List method append adds item to the end of the list. If you want put item to specific place then you can use insert method which inserts an item at a given position.

>>> items = list()
>>> items.append(['b'])        # append ['b'] to end of items
>>> items
[['b']]
>>> items.insert(0, ['a'])     # insert ['a'] at index 0 to items
>>> items
[['a'], ['b']]
Thank you guys, it worked; i used append([]) and it was exactly what i needed, very simple Smile