Define a Global List in Python

In this tutorial, you’ll learn how to define a global list in the Python programming language.

The table of contents is structured as follows:

Let’s dive into it!

Introduction to Global Lists

In Python, a global list is a list that is defined outside of any function or class. It is accessible from anywhere in the program and can be modified or accessed by any function or class within the same scope.

Defining a Global List

To define a global list in Python, you can follow these steps:

Step 1: Declare the list object outside of any function or class.

Step 2: Assign values to the list.

Here’s an example of defining a global list:

# Step 1: Declare the global list
my_global_list = []
 
# Step 2: Assign values to the list
my_global_list.append(1)
my_global_list.append(2)
my_global_list.append(3)
 
# Access the global list
print(my_global_list)  # Output: [1, 2, 3]

In the above example, we declared a global list named my_global_list and assigned values to it using the append() method. We can access the global list from any part of the program.

Accessing and Modifying the Global List

Once you have defined a global list, you can access and modify it from any function or class within the same scope.

Here’s an example that demonstrates accessing and modifying the global list:

# Declare the global list
my_global_list = []
 
# Append values to the list
my_global_list.append(1)
my_global_list.append(2)
my_global_list.append(3)
 
# Access and modify the global list
def modify_list():
    global my_global_list
    my_global_list.append(4)
    my_global_list.remove(2)
 
# Call the function to modify the list
modify_list()
 
# Print the modified list
print(my_global_list)  # Output: [1, 3, 4]

In the above example, we declared a global list my_global_list and appended values to it. Inside the modify_list() function, we accessed the global list using the global keyword and modified it by appending a new value and removing a specific value.

Video, Further Resources & Summary

Do you need more explanations on how to define a global list? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

The YouTube video will be added soon.

Furthermore, you could have a look at some of the other tutorials on Statistics Globe:

This post has shown how to define and work with a global list in Python. In case you have further questions, you may leave a comment below.

 

Ömer Ekiz Informatics Expert

This page was created in collaboration with Ömer Ekiz. You may have a look at Ömer’s author page to read more about his academic background and the other articles he has written for Statistics Globe.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.

Top