1
def nested_list(nested):
for i in range (0, len(nested)):
    for k in range (0, len(nested)):
        print(nested[i][k], end = " ")

nested_list([[1,2,3],[4,5,6],[7,8,9]])

output : 1 2 3 4 5 6 7 8 9

İt is working. But when I change nested_list([[1,2,3,4],[5,6],[7,8,9,10]])like this I get an error. What is the best solution to fix this problem ?

  • 1
    Does this answer your question? [How to make a flat list out of a list of lists](https://stackoverflow.com/questions/952914/how-to-make-a-flat-list-out-of-a-list-of-lists) – Maurice Meyer Dec 16 '21 at 13:45
  • ... or this: https://stackoverflow.com/questions/2158395/flatten-an-irregular-list-of-lists – Maurice Meyer Dec 16 '21 at 13:46
  • Yes sir. But I haven't learned those commands yet. @MauriceMeyer – Jack Harmon Dec 16 '21 at 13:49
  • What kind of error is printed? Please add the full and exact error message to your question, along with your attempts to resolve that error – Nico Haase Dec 17 '21 at 07:45

2 Answers2

1

You get an error because your original code assumes "square" list (sublists of the same length like full list).

You need to change inner for loop to check for len of current sublist, not whole list:

def nested_list(nested):
    for i in range(len(nested)):
        for k in range(len(nested[i])): # check len of current sublist
            print(nested[i][k], end = " ")

Also changed range(0, len(nested)) to just range(len(nested)). Range works both as range(start, stop[, step]) (if no step is given, 1 is the default) and range(stop) which starts from 0. :)

range signatures in builtin functions list, real description of how range works

h4z3
  • 5,265
  • 1
  • 15
  • 29
  • I understand sir. Thank you :) – Jack Harmon Dec 16 '21 at 13:44
  • Not a sir. @JackHarmon – h4z3 Dec 16 '21 at 13:47
  • Sorry ma'am. Thank you again – Jack Harmon Dec 16 '21 at 13:51
  • @JackHarmon Not a ma'am either... It's better to avoid gendered language altogether if you don't know who you're talking to. Especially since English second person is already neutral. – h4z3 Dec 16 '21 at 13:57
  • You are right. I'll pay attention thank you again. – Jack Harmon Dec 16 '21 at 14:00
  • @h4z3, I've just submitted an edit with a little fix about `range` first parameter. Usefull answer, if I remember well it's less efficient than the `sum` I suggested, but it's more pythonic. +1 – FLAK-ZOSO Dec 16 '21 at 14:24
  • @FLAK-ZOSO I tried to modify as little as OP's code as I could because it seems like a learner's code, so I understand why OP might prefer more verbose ranges. ;) – h4z3 Dec 17 '21 at 09:47
  • @FLAK-ZOSO I changed it to a longer description on range with links - in case more learners come. And fun fact: range is a class, not a function! (your comment said it's a function :P) And it has two signatures because it's written in C and it's the first argument that has a default value, so cpython devs made it clear with parameter names as well. – h4z3 Dec 17 '21 at 10:03
  • I didn't know that range was a class, I've always thought it was a not-editable iterable, in that case I thought it was a function that returned a tuple. Thank you for the explaination. – FLAK-ZOSO Dec 17 '21 at 14:19
0

There is a fast way to do this:

nested_list = [[1,2,3],[4,5,6],[7,8,9]]
print(sum(nested_list, []))

The sum Python's built-in function can be used to "sum" (and in this case to concatenate) the elements in an iterable.

FLAK-ZOSO
  • 3,873
  • 4
  • 8
  • 28