-1

I want to adjust lists to the same length but the if-elif statement is not working as I thought, its very weird. This might be something so obvious

Code:


l = [1,2,1,4,3,1]
l2 = [1,5,7,3,35,5,66,7]
lenl = len(l)
lenl2 = len(l2)
if l < l2:
    l_l2 = lenl - lenl2
    list1 = l2
    list2 = l
elif l > l2:
    l_l2 = lenl2 - lenl
    list1 = l
    list2 = l2
for i in range(0,l_l2):
    list1.append(None)
print(list2)
print(list1)
for i in range(0,l_l2):
    list1.remove(None)
print(list1)

I keep getting:


[1, 2, 1, 4, 3, 1]
[1, 5, 7, 3, 35, 5, 66, 7]
[1, 5, 7, 3, 35, 5, 66, 7]

What I want:


[1, 2, 1, 4, 3, 1,None,None]
[1, 5, 7, 3, 35, 5, 66, 7]
[1, 5, 7, 3, 35, 5, 66, 7]

Zen35X
  • 76
  • 2
  • 10
  • use else and that way you always get either the if block (if True) or the else block (if False) – mj_codec Aug 31 '22 at 00:25
  • The `if` and `elif` statements are comparing `l` and `l2` rather than `lenl` and `lenl2`. – John Kugelman Aug 31 '22 at 00:25
  • Okay, so, look at the code closely. The first thing that will be `print`ed, which apparently is wrong, is `list2`, yes? And you are expecting that `None` values will be `.append`ed, and thus appear in the `print` result, because of... the previous loop, right? The one that says `for i in range(0,l_l2): list1.append(None)`? Why would that append elements to `list2`, when it says `list1.append`? – Karl Knechtel Aug 31 '22 at 00:28
  • 2
    There are many things in this code that don't make sense, all logical errors and oversights that are unlikely to help anyone else solve a practical problem. Please read https://ericlippert.com/2014/03/05/how-to-debug-small-programs/, and think more clearly about how the code is intended to work. Try writing out everything step by step in complete English sentences first. You may find it helpful to use a real pencil and piece of paper for this. – Karl Knechtel Aug 31 '22 at 00:30
  • @KarlKnechtel I want to see list2 which is the one that is the biggest none values will be appended to list1 until its the same length as list2 – Zen35X Aug 31 '22 at 00:31
  • Yes, I understand what you want. But if you append values to `list1`, and then try `print(list2)`, then of course you won't see the appended values unless they name the same list (which they don't, here). (Also, consider using names that make your logic more clear. Maybe `shorter` and `longer`?) – Karl Knechtel Aug 31 '22 at 00:32
  • @KarlKnechtel im printing both? at first i print both then at the end i just print list1 without the none for testing – Zen35X Aug 31 '22 at 00:33
  • The *first* print is `list2`, and that's the one where the result is different between the desired output and the actual output. – Karl Knechtel Aug 31 '22 at 00:34
  • @JohnKugelman yes, damn now im just even more confused – Zen35X Aug 31 '22 at 00:35
  • 2
    Again: start by using names that make sense and explain the purpose of each thing in the program. Write the intended logic out separately, in complete English sentences. Go through the code and compare what you have to the English, step by step. And read https://ericlippert.com/2014/03/05/how-to-debug-small-programs/. – Karl Knechtel Aug 31 '22 at 00:36
  • You have the l_l2 value written all wrong. When L>L2, L_l2 should be lenl - lenl2 and when l2> l, it should be lenl2 - lenl. In your case the value of l_l2 would be -2, and hence, it never goes in the append loop. Also, in your logic, you can't list.remove more than once. Remove function removes all matching values, so your code will throw an error – vigviswa Aug 31 '22 at 00:37
  • Start naming variables correctly, it will go a long way in understanding your code easily. – vigviswa Aug 31 '22 at 00:38
  • @vigviswa not in my case remove function only removes the last value so i used the for loop to run it more than once and its working fine – Zen35X Aug 31 '22 at 00:40
  • On the other hand, if you just want to solve the problem, rather than debug the code, [start](https://meta.stackoverflow.com/questions/261592) by [looking around](https://duckduckgo.com/?q=python+pad+lists+to+the+same+length) for an existing solution. For example, does https://stackoverflow.com/questions/3438756/ answer your question? – Karl Knechtel Aug 31 '22 at 00:47

2 Answers2

2

Your main issue is comparing two lists instead of using the length of each. Also, I feel like you swapping the lists might've confused you and propagated down to the rest of the code (lines 8 & 9 and 13 & 14 -> affected lines 16 & 20). Nevertheless, I just fixed some parts and think it should work now for what you want it to do. Also, you might want to double-check your math for getting the length.

l = [1,2,1,4,3,1]
l2 = [1,5,7,3,35,5,66,7]
lenl = len(l)
lenl2 = len(l2)
if lenl < lenl2:
    l_l2 = abs(lenl - lenl2)
    list1 = l2
    list2 = l
elif lenl > lenl2:
    l_l2 = abs(lenl2 - lenl)
    list1 = l
    list2 = l2
for i in range(0,l_l2):
    list2.append(None)
print(list2)
print(list1)
for i in range(0,l_l2):
    list2.remove(None)
print(list1)
tsusdere
  • 51
  • 3
  • It works, but when l2 is smaller than l it doesnt Output: ```python [1, 5] [1, 2, 1, 4, 3, 1] [1, 2, 1, 4, 3, 1] ``` it worked when l was smaller but when it was bigger it didnt work – Zen35X Aug 31 '22 at 00:42
  • 1
    @Zen35X It's due to the math attempting to get the difference in length between the two lists. I have updated it now so it should work, you might want to double-check why it works. – tsusdere Aug 31 '22 at 00:46
-1

Thefor loops never run because l_l2 is defined in the scope of the ifs, but not outside. In short, there is no such variable l_l2 unless you are inside the ifs.

This code should do it:

l = [1,2,1,4,3,1]
l2 = [1,5,7,3,35,5,66,7]
lenl = len(l)
lenl2 = len(l2)
global l_l2
if l < l2:
   l_l2 = lenl - lenl2
   list1 = l2
   list2 = l
elif l > l2:
    l_l2 = lenl2 - lenl
    list1 = l
    list2 = l2
l_l2 = abs(l_l2)
for i in range(0,l_l2):
    l.append(None)
print(list2)
print(list1)
print(list1)
Random
  • 57
  • 8