Python Basics — Working with Lists (cont.)

Mehdi Lotfinejad
5 min readDec 3, 2021
Photo by Raphael Schaller on Unsplash

In this lesson, we will learn more methods for manipulating lists in Python. First, we will discuss variable storage in Python and then how to make a copy of a list. We also will learn how to sort the data stored in a list. We will work with numerical lists and iterate over lists with loops at the end of this lesson.

How do Variables work?

Once you declare a variable, its value will be stored in a location in memory. This location has a specific reference ID that can be retrieved with the id() function. First, let's declare a variable and check its storage location.

Go ahead and run the cell. It outputs a large number that represents a specific location in memory.

Now let’s see what happens if we assign the x variable to another variable.

In the above code, y = x will create another reference variable y which will refer to the same memory location, and both variables have the same location identifier.

In the following example, let’s create a list and assign it to a variable, and see how it will be stored in memory.

a_list = [1,2,3] b_list = a_list print(a_list) print(b_list) if id(a_list) == id(b_list): print("Both refer to the same memory location.")

Go ahead and run the cell. It outputs the values of these lists and, in the end, prints them out: Both refer to the same memory location,which means they share the same memory location.

Now, let’s change the first value of the a_list list and print out their values.

a_list[0] = 100 print(a_list) print(b_list) if id(a_list) == id(b_list): print("Both refer to the same memory location.")p

Go ahead and run the cell. The output is clear. Because both lists use the same memory location, changing the a_list list affects the b_list list.

Copying a List

But sometimes, we need to copy a list, which means any alteration in the primary list does not affect the copied one. To do so, you need to use the copy() method. Let's see how it works.

a_list = [1,2,3] b_list = a_list.copy() print(a_list) print(b_list) if id(a_list) == id(b_list): print("Both refer to the same memory location.") else: print("They point to different memory locations.")

Go ahead and run the cell. As you can see, both lists have the same values, but they point to different memory locations.

Now we have two lists. We use the copy() method, if we need the original list unchanged when the new list is modified.

Working with Numerical Lists

Sometimes, we want to return the minimum, maximum, or summation of the elements in a list. Python provides these functions for us. Let’s see how we can apply these three functions on numerical lists.

num_list = [2,7,1,8,10] print("Minimum =",min(num_list)) print("Maximum =",max(num_list)) print("Sum =",sum(num_list))

Go ahead and run the cell. The min() and max() built-in functions return the minimum and maximum number as their names state. The sum() function adds all the numbers up.

Sorting a List

Python provides a few functions and methods for sorting list items. The sorted() function sorts a list without altering the original list, copies the original list, sorts it, and returns the sorted list as a new list. However, the sort() method alters the original list and sorts it directly.

The sorted() Function

num_list = [1,0,5,9,4] print(sorted(num_list)) print(num_list)

Go ahead and run the cell. The sorted list is returned without altering the original list, and the num_list is still in the original order as we declared it.

The sort() Method

num_list = [1,0,5,9,4] num_list.sort() print(num_list)

Go ahead and run the cell. The result is a sorted list. The num_list variable is changed, as .sort() directly changes the value.

The in and not in Keywords

Basically, the in operator checks whether a specified value is in a collection like a list or not.
When the in operator is used in a condition, the statement returns a Boolean value evaluating into either True or False. When the specified value is found inside the collection, the statement returns True; otherwise, it returns a False.

fruits = ["apple","orange","cherry","pineapple"] if "cherry" in fruits: print("Found.") if "peach" not in fruits: print("Not Found.")

Go ahead and run the cell. The first condition is evaluated to True since “cherry” is in the list of the fruits and prints “Found.”, the second condition is evaluated to True, too, since “peach” is not in the list, so it outputs “Not Found.”.

If you want to check whether a list is empty or not, you can simply write the following condition:

if not num_list:

If the list is empty, the condition evaluates to True; otherwise, it evaluates to False.

num_list = [] if not num_list: print("The list is empty.")

Go ahead and run that cell. It outputs "The list is empty", since the num_list is empty (empty brackets).

For Loops and Lists

We can use for loop for iterating over the items in a list. To do so, we use a temporary variable, the in keyword, and the list's name. In each iteration, the temporary variable is assigned the item's value. Let's check it out:

fruits = ["apple","orange","cherry","pineapple"] for fruit in fruits: print(fruit)

Go ahead and run the cell. It outputs each item within the list. During the first iteration, "apple" is assigned to the temporary variable fruit, and once it prints it out, it moves on to the next item.

There are much more lists methods in Python; I’ve tried to cover the most frequent ones. If you are interested in learning more about list methods, take a look at the w3schools website.

Alright, I hope you enjoyed this lesson. In the next lesson, you will learn to define functions in Python.

Learn Python Basics for Data Analytics

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Mehdi Lotfinejad
Mehdi Lotfinejad

No responses yet

Write a response