Python Forum
Append list into list within a for loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Append list into list within a for loop
#1
Hi, I would like to append lists into one list in a for loop. See my code bellow:

def triangle(n: int):
    pole = []
    seznam = []
    i = 0
    j = 0  

    while j < n :          
        while i <= j:
            a = pole.append(1)
            print(pole)  
            seznam.append(a)            
            i +=1 
        j +=1
            
    print(seznam)

####END
My output for triangle(4) is
Output:
[1] [1, 1] [1, 1, 1] [1, 1, 1, 1] [None, None, None, None]
But desired output for triangle(4) is:
Output:
[[1], [1, 1], [1, 1, 1], [1, 1, 1, 1]]
Can you help me, how can I replace "None" with lists of 1? Thank you very much!
Reply
#2
Please put your posted code inside python tags. Otherwise, we cannot read the code properly.

The expression a = pole.append(1) seems incorrect.

pole.append(1) will add a new element to the list "pole". But it will not return anything. "a" will be None after this operation. What do you want "a" to be?

>>> a = ['some', 'list'].append(1)
>>> a == None
True
Reply
#3
list.append(item) does not return a list. The reason for this is append modifies the list instead of creating a new list.

Your function should return a list but it doesn't. You throw the list away as soon as the function exits. You try to get the list by using a variable that has the same name (seznam) as a local variable in the function, but will not work. This doesn't matter much because you never call your triangle function anyway.

You are writing this program in Python, so use Python loops, not C loops.
five_a = []
for a in 'aaaaa':
     five_a .append(a)
# or
for _ in range(5):
    five_a .append('a')
There are shorter ways than using a loop to initialize a list. You can use a list comprehension:
five_a  = ['a' for _ in range(5)])
Or you can use an initializer.
five_a = (['a']*(5))
To get a list of lists you could have a loop inside of a loop, or you could put an list comprehension or an initialize inside a loop. You could also use a list comprehension that uses a generator to make the inner and outer lists. Or you could use a generator generator inside of a generator. Your triangle generator program can be written in a single line of Python code.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why changing data in a copied list changes the original list? plumberpy 3 360 Aug-14-2021, 02:26 AM
Last Post: plumberpy
  Compile list of dictianories out of another list of dictianories by certain keys CatorCanulis 10 906 Jun-10-2021, 08:35 PM
Last Post: perfringo
  Looking for a way to loop until user enters from a list? PythonW19 7 837 Mar-21-2021, 08:56 PM
Last Post: PythonW19
  Order a list with successive permutations based on another list yvrob 3 747 Mar-19-2021, 08:20 AM
Last Post: supuflounder
  Saving list in a list quest_ 3 893 Mar-10-2021, 09:58 AM
Last Post: quest_
Star Convert Bytearray into List using list() Shlok 2 651 Feb-18-2021, 10:44 AM
Last Post: deanhystad
  How to append multiple <class 'str'> into a single List ahmedwaqas92 2 709 Jan-07-2021, 08:17 AM
Last Post: ahmedwaqas92
  For Loop with List Comprehension muzikman 25 1,836 Dec-18-2020, 10:45 PM
Last Post: muzikman
  Adding List Element if Second part of the List Elements are the Same quest_ 3 748 Nov-25-2020, 04:33 PM
Last Post: bowlofred
  Count number of occurrences of list items in list of tuples t4keheart 1 714 Nov-03-2020, 05:37 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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