Python List Tutorial (5)

Python List Tutorial
Python List Tutorial

Python list – Python is a highly popular programming language for application development. Let’s continue our journey by looking at Python lists and how to manipulate them.

Chapter before : #4 – Conditions

Python List

Lists are essential components to understand in Python because they are incredibly useful. They allow you to create a container for multiple different values, unlike regular variables we’ve seen before, which can only hold a single value.

Here’s an example of creating a list:

users = ["czero", "batman", "robin", "robocop"]
print(users)

It will display our list like this:

["czero", "batman", "robin", "robocop"]

Lists are considered a different type than the ones we’ve seen before. To determine the type, you can use the type() function like this:

users = ["czero", "batman", "robin", "robocop"]
print(type(users))

This program will return List.

Accessing a Specific Element of the List

To display a specific element of our list, you should use the following convention with [] and an index. The first element in the list has an index of 0, so the second element has an index of 1.

users = ["czero", "batman", "robin", "robocop"]
print(users[0])

This will display czero.

You can also retrieve multiple elements defined by a specific range. You can use a convention to display elements from index X to element Y. It’s worth noting that the second value in the range is the position of the element, not an index.

users = ["czero", "batman", "robin", "robocop"]
print(users[1:3])

This will display ["batman", "robin"].

You can omit the first value to display elements from the beginning up to element Y.

users = ["czero", "batman", "robin", "robocop"]
print(users[:3])

This will display ["czero", "batman", "robin"].

It’s also possible to omit the second value to display elements from index X to the last element.

users = ["czero", "batman", "robin", "robocop"]
print(users[1:])

This will display ["batman", "robin", "robocop"].

Counting the Number of Elements in the List

To find the number of elements in a list, you can use the len() function. This function also gives you the index of the last element in the list, but don’t forget to subtract 1 since indexes start from 0.

users = ["czero", "batman", "robin", "robocop"]
print(users[len(users) - 1])

This will display robocop.

However, there’s another convention to select the last element of a list:

users = ["czero", "batman", "robin", "robocop"]
print(users[-1])

This will also display robocop.

Modifying a List

To modify an element in a list, you need to target it by its index and assign a new value to it:

users = ["czero", "batman", "robin", "robocop"]
users[1] = "joker"
print(users)

This will display ["czero", "joker", "robin", "robocop"].

You can also modify multiple elements at once using the previously mentioned convention, for example, [1:3].

users = ["czero", "batman", "robin", "robocop"]
users[1:3] = ["joker", "superman"]
print(users)

This will display ["czero", "joker", "superman", "robocop"].

Inserting a New Element into the List

With the insert() function, you can easily add an element at the desired index, shifting the other elements in the list:

users = ["czero", "batman", "robin", "robocop"]
users.insert(2, "joker")
print(users)

This will display ["czero", "batman", "joker", "robin", "robocop"].

If you only want to add an element to the end of the list, you don’t need to use len() to define the new index. Simply use the append() function to add an element at the end of the list:

users = ["czero", "batman", "robin", "robocop"]
users.append("joker")
print(users)

This will display ["czero", "batman", "robin", "robocop", "joker"].

It’s also possible to add multiple elements at once using the extend() function by providing a list to be added to the original one:

users = ["czero", "batman", "robin", "robocop"]
users.extend(["joker", "superman"])
print(users)

This will display ["czero", "batman", "robin", "robocop", "joker", "superman"].

Deleting an Element from the List

To remove an element from the list and reassign indexes to the following elements, you can use the del statement provided by Python:

users = ["czero", "batman", "robin", "robocop"]
del users[1]
print(users)

This will display ["czero", "robin", "robocop"].

You can also use the convention that defines a range to delete multiple elements at once:

users = ["czero", "batman", "robin", "robocop"]
del(users[1:3])
print(users)

This will display ["czero", "robocop"].

Some may prefer using users.pop(1) instead of del users[1], as it will remove an element from the list in the same way.

Python also allows you to remove an element by its value rather than its index using the remove() function:

users = ["czero", "batman", "robin", "robocop"]
users.remove("batman")
print(users)

This will display ["czero", "robin", "robocop"].

To completely empty your list, you can use the following code:

users = ["czero", "batman", "robin", "robocop"]
del users[:]
print(users)

This will display an empty list: [].

The users.clear() function accomplishes the same thing. You can choose the method you prefer.

Combining Two Lists

In Python, it’s possible to combine two lists. Simply use the + operator between the two lists. Here’s a simple example to understand:

users1 = ["czero", "batman"]
users2 = ["robin", "robocop"]
users3 = users1 + users2
print(users3)

This will display: ["czero", "batman", "robin", "robocop"].

You can also achieve the same result using the same list with the * operator. This will add the list to itself the specified number of times.

users1 = ["czero", "batman"]
users2 = users1 * 2
print(users2)

This will display: ["czero", "batman", "czero", "batman"].

Finding an Element in the List

In Python, you can also search for a specific element in the list using the in keyword. If the element exists in the list, the program will return True; otherwise, it will return False.

users = ["czero", "batman", "robin", "robocop"]
print("czero" in users)

Since “czero” is present in the list, it will return True.

Memory Allocation Caution

In Python, you need to be careful when manipulating lists. Assigning one list to another with the = operator won’t create a copy of the list but will instead reference the same memory location. This means that any modifications to one of the variables will also affect all the variables referencing that memory location.

users1 = ["czero", "batman", "robin", "robocop"]
users2 = users1
users1[1] = "superman"
print(users2)

This will display: ["czero", "superman", "robin", "robocop"].

To copy a list into another variable (creating an independent memory block), you should use the following convention:

users1 = ["czero", "batman", "robin", "robocop"]
users2 = users1[:]
users1[1] = "superman"
print(users1)
print(users2)

This will display two different contents, unlike the previous example where only the memory address is copied.

Conclusion – Python List

We will delve deeper into lists in the next chapter, as they offer many more secrets to uncover.

Next chapter :#6 – Functions

(Visited 78 times, 1 visits today)
About Judicaël Paquet 368 Articles
Judicaël Paquet (agile coach and senior devops) My Engagements in France and Switzerland: - Crafting Agile Transformation Strategies - Tailored Agile Training Programs - Raising Awareness and Coaching for Managers - Assessing Agile Maturity and Situational Analysis - Agile Coaching for Teams, Organizations, Product Owners, Scrum Masters, and Agile Coaches Areas of Expertise: Scrum, Kanban, Management 3.0, Scalability, Lean Startup, Agile Methodology.

Be the first to comment

Leave a Reply

Your email address will not be published.


*