Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
adding lists to lists?
#1
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..
Reply
#2
itens.append('A')
without the quotes, it's not a string
Reply
#3
(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?
Reply
#4
A = 'a'
B = 'b'
items = []
items.append([A])
print(items)
items.append([B])
print(items)
Output:
[['a']] [['a'], ['b']]
Reply
#5
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']]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#6
Thank you guys, it worked; i used append([]) and it was exactly what i needed, very simple Smile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem with print lists MarekGwozdz 4 612 Dec-15-2023, 09:13 AM
Last Post: Pedroski55
  python convert multiple files to multiple lists MCL169 6 1,436 Nov-25-2023, 05:31 AM
Last Post: Iqratech
  Lists blake7 6 693 Oct-06-2023, 12:46 PM
Last Post: buran
  Trying to understand strings and lists of strings Konstantin23 2 697 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Why do the lists not match? Alexeyk2007 3 763 Jul-01-2023, 09:19 PM
Last Post: ICanIBB
  ''.join and start:stop:step notation for lists ringgeest11 2 2,379 Jun-24-2023, 06:09 AM
Last Post: ferdnyc
  Need help with sorting lists as a beginner Realist1c 1 712 Apr-25-2023, 04:32 AM
Last Post: deanhystad
  Pip lists the module but python does not find it Dato 2 1,232 Apr-13-2023, 06:40 AM
Last Post: Dato
  Generate lists of devices and partitions from /proc/partitions? DachshundDigital 1 730 Feb-28-2023, 10:55 PM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 878 Feb-23-2023, 02:21 PM
Last Post: sparkt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020