Skip to content

Define Global list in Python | Example code

You can declare a Global list on the file/module level like my_global_list = list() in Python. If you want to append to it inside a function you can use the global keyword.

Example global list in Python

Simple example code.

aList = []


def bar():
    sublist = 1
    aList.append(sublist)


bar()
print(aList)

Output:

Define Global list in Python

Another example

Declare a global list and use it in function.

global a
a = [1, 2, 3]


def update_a():
    del a[1:2]
    print(a)


update_a()

Output: [1, 3]

Do comment if you have any doubts or suggestions on this Python list.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

1 thought on “Define Global list in Python | Example code”

Leave a Reply

Your email address will not be published. Required fields are marked *