Define Global list in Python | Example code

You can declare 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



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]


Output: [1, 3]

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

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

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

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.