Learning Python - Part 5 - Lists - Changing, Adding, and Removing Elements

Untitled-1-Recovered.jpg

Learning Python - Part 1 - Familiarise yourself with the Python Console and write your first Python program!

Learning Python - Part 2 - Declaring Variables and understanding strings

Learning Python - Part 3 - Comments

Learning Python - Part 4 - Introducing Lists


Hello again, "Future Programmer" if it is the first time that you read any of my posts I recommend you to visit my old "Learning Python" posts. And without further ado, let's get into it!

Changing, Adding, and Removing Elements



Most lists you create will be dynamic, meaning you’ll build a list and then add and remove elements from it as your program runs its course.

Modifying elements in a List



The syntax for modifying an element is similar for accessing one.

For example:

participants = ['jacob', 'bella', 'mark']
print(participants)

participants[0] = 'edward'
print(participants)

As you can see the syntax to modify elements of a list is slightly different than accessing one. The output will be the following:
['jacob', 'bella', 'mark']
['edward', 'bella', 'mark']

You can change the value of any item in a list, not just the first item.

Adding Elements to a List

Appending Elements to the End of a List


Some of the ways to add an item to the end of the list is by using the "append()" method.
For example:
participants = ['jacob', 'bella', 'mark']
print(participants)

participants.append('sven')
print(participants)

The name "sven" will be appended to the list like this:
['jacob', 'bella', 'mark', 'sven']

Inserting Elements into a List


With the insert method, you can add items in your list but by choosing its position.
For example:
participants = ['jacob', 'bella', 'mark']

participants.insert(0, 'poppy')
print(participants)

The output will be:
['poppy', 'jacob', 'bella', 'mark']

Removing Elements from a List


Often, you’ll want to remove an item or a set of items from a list. For example, when a user decides to cancel their account on a web application you created, you’ll want to remove that user from the list of active users. You can remove an item according to its position in the list or according to its value. If you know the position of the item you want to remove from a list, you can use the del statement.
participants = ['jacob', 'bella', 'mark']
print(participants)

del participants[0]
print(participants)

The outpu will be:
['jacob', 'bella', 'mark']
['bella', 'mark']

If you dont know the position of the item, but know its value you can just remove it with its value:
participants = ['jacob', 'bella', 'mark']
print(participants)

participants.remove('bella')
print(participants)

The result will be:
['jacob', 'bella', 'mark']
['jacob', 'mark']

Removing an Item Using the pop() Method

Sometimes you might want to delete an item from a list and store it in a variable in case you need it later.
For example:
participants = ['jacob', 'bella', 'mark']
print(participants)

popped_participant = participants.pop()
print(participants)
print(popped_participant)

The output will be:
['jacob', 'bella', 'mark']
['jacob', 'bella']
mark

You can add the position of the item you wanna pop by adding its position to the pop methode: ......pop(1)

This is all for today's post, hope you learned something from it, dont forget to upvote and follow me if i was of any help, if you didnt understand something just say it in the comments section, see you in the next post!