Add a New Sublist to an Existing List

5813 picture 5813 · Oct 1, 2013 · Viewed 8.2k times · Source

Everyone!

I am trying to add a new sublist to an existing list, but I am not quite sure on how to do it. Here is my code:

data = [[4,5],[3,7]]
search = 9
for sublist in data:
    if search in sublist:
        sublist.append(0)
        print("there", sublist)
        break
    else:
        print("not there")
        break
        def sublist():
            [5,6]
            print[data]

However, if the search is not there, the sublist does not get added to the original list. How can I do this?

Cheers! 5813

Answer

dawg picture dawg · Oct 1, 2013

Just append it:

>>> data = [[4,5],[3,7]]
>>> data.append([5,6])
>>> data
[[4, 5], [3, 7], [5, 6]]