1

I am new to programming and trying to understand the syntax.

in my example, i would like to have a result of: table = [[1,2,3], [2,4,6], [3,6,9]] but somehow i get and empty list. I am not sure where to fix it.

row = [1, 2, 3]
col = [1, 2, 3]
li = []
table = []
for x in row:
    for n in col:
        li.append(x*n)
    table.append(li)
    li.clear()

print(table)
3
  • 2
    Why are you doing li.clear()? Commented Apr 29, 2020 at 17:11
  • 1
    table.append(li[:]) - if you clear the same list that you're appending to the table, obviously you'll end up with []. Append a copy instead Commented Apr 29, 2020 at 17:12
  • @yatu, he was doing it to get the output described, as opposed to: [[1,2,3],[1,2,3,2,4,6],[1,2,3,2,4,6,3,6,9]] Commented Apr 29, 2020 at 17:23

4 Answers 4

2

Under the hood, lists are pointers. This means that when you append a list to another list, it is not creating a new list, it is using the same list. So whenever you do li.clear, it gets rid of the internal list elements...

you're gonna want this instead...

row = [1, 2, 3]
col = [1, 2, 3]
table = []
for x in row:
    li = []
    for n in col:
        li.append(x*n)
    table.append(li)

print(table)

Create a new list each time, so it's a separate object in memory for each append operation

Sign up to request clarification or add additional context in comments.

1 Comment

Upvote for providing not only a solution but also an explanation.
0

using your own code:

row = [1, 2, 3]
col = [1, 2, 3]
table = []
for x in row:
    li = []
    for n in col:
        li.append(x*n)
    table.append(li)

print(table)

you will then get:

output

1 Comment

Answer is correct but it would be more helpful if you explain why it works.
0
row = [1,2,3]
col = [1,2,3]
table = []

for x in row:
  li = []
  for n in col:
    li.append(x*n)
  table.append(li)

The difference is making the li = [] variable inside the first level of your for loop. This essentially "clears" it out each time.

Comments

0

You need to look into what is mutabe and immutable in python

Let me explain in brief what happens in your code

row = [1, 2, 3]
col = [1, 2, 3]
li = []
table = []
for x in row:
    for n in col:
        li.append(x*n)
    table.append(li) # everything is good until here
    li.clear() # but here you are sayning that li is empty so it also goes and changes table.append(li) to table.append(li.clear()), because li is a list and lists are mutable objects. 
# what you can do is create another new object (which happens to be named li: li=[] so the table.append(li) will not change to table.append([])

change your code to

row = [1, 2, 3]
col = [1, 2, 3]
li = []
table = []
for x in row:
    for n in col:
        li.append(x*n)
    table.append(li)
    li = []

print(table)

1 Comment

my thought process was really just the flow of the code, i didnt know clear would still affect the upper part. I guess my understanding about clear is not enough.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.