Python Forum
adding lists to lists? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: adding lists to lists? (/thread-17405.html)



adding lists to lists? - ivinjjunior - Apr-09-2019

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..


RE: adding lists to lists? - Larz60+ - Apr-09-2019

itens.append('A')
without the quotes, it's not a string


RE: adding lists to lists? - loomski - Apr-09-2019

(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?


RE: adding lists to lists? - Yoriz - Apr-09-2019

A = 'a'
B = 'b'
items = []
items.append([A])
print(items)
items.append([B])
print(items)
Output:
[['a']] [['a'], ['b']]



RE: adding lists to lists? - perfringo - Apr-10-2019

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']]



RE: adding lists to lists? - ivinjjunior - Apr-15-2019

Thank you guys, it worked; i used append([]) and it was exactly what i needed, very simple Smile