What Were You Doing at Fifteen?

While I struggled with braces on my teeth and conjugating the past tense in French and German, Michelangelo was carving his first works. The braces were not a pretty sight. The Madonna della Scala…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Lists in Python

Lists in Python are very handy in maintaining elements that may or may not be similar in a particular order that you assign them. You can create lists of grocery store locations, items being sold online, or even favorite fruits! Luckily there are simple methods that we can employ to create and modify lists to our liking. We will cover a number of them below. We will be using a list of fruits to use as an example.

When we print our list of fruits, we are sent a list that looks similar to the input we currently have. In order to retrieve items from our list individually, we let Python know exactly what element we are looking for in our list by stating the position in which our element we are seeking is at. If we look at our list we have a total of eight items.

We can do a quick check by using len()

Checks out!

It is important to remember that Python, just like other programming languages, the first element in a list is in position 0 and NOT position 1. So for our example, the elements would be apples in position 0, bananas in 1, blueberries in 2 and so on. With this in mind lets ask for elements in positions 5 and 7.

If we count out the values we can see that it all checks out. Another quick tip, if you want to see the final element in a list, which is helpful when you have a large list you can use negative numbers. For example:

This also works for the second to last and so forth.

From here you can use individual elements as you’d like. For example, we can call them into a message.

Manipulating elements in a list

There are times where we may want to add, remove or alter the elements in our list to fit our needs. We will continue using our list of fruits to see how we can change our elements based on our needs.

We can change any element in a list to another value by calling the specific element in our list and telling Python to assign it with the value that we would like instead. In the example below, we will be changing the element in fruits[3], cherries, to cantaloupe.

What if we forgot to add a fruit to our list? Python makes it very easy to add a new element to the end of a list using .append(). Remember, .append() will add a new element at the end of the list.

We can also use .append() to create lists as we go, which in some situations can be super helpful! What if we were asked to catalog the most liked berries. Below, we will create the list berries and tell Python it is an empty list. Then we go ahead and add different types of berries to this list as shown below. At the end of our code we will end with a list that includes blueberries, blackberries, raspberries, and strawberries.

We can also replicate our previous fruits list from our example using .append(), just like our berries list above.

Now, maybe we want to put cherries back on our list, which we had previously changed to cantaloupe. At the same time we want to keep our list in alphabetical order; how do we go about this? We can easily insert cherries back in the list by using .insert(). Since we want to place cherries as the fourth element in our list we will use the following code:

Adding and changing elements is great, but what if we wanted to remove elements from the list? Maybe figs fell out of favor of being one of the most liked fruits. There are three different methods we can use to remove elements from a list, one that accomplishes completely and one that allows the removed element to be used in the future.

Let’s say for example that upon creating our list of fruits that cantaloupe should not have been added. We can use the del method to remove the element, while calling the position that cantaloupe is in.

At this point the element cantaloupe is no longer available for our use.

What if we wanted to use an element from our list of fruits after we have removed it? We can use the second method for element removal, .pop() to achieve this. It is important to keep in mind that when you use the .pop() method, you are removing the last element in a list. If you wanted to remove a specific element, you can specify its location in the brackets after .pop.

Example

As we can see, mango was the final element on our list, which was removed.

In the following bit, we will be removing the fourth element of the list, which is cherries, to demonstrate how we can specify which position we wish to remove.

The third method we have is helpful when we are working with a large list and have no idea what the position number is. Say that we wanted to remove grapes from our list of fruits but had no idea its position. We can use the .remove() method by defining the element that we want to remove in the brackets. Let’s remove grapes from our list.

*As a special note you can still work with the removed element by assigning it to another list.

That concludes some of the fun and entertaining things you can do when creating lists with elements. There are unlimited ways that one can use these methods with their data so have fun and give them a try!

Add a comment

Related posts:

Why your dreams are meaningful.

Dreams have been here as long as we have. Literally! They are an integral part of our human experience. Some of us dream more and some less frequent. But we all have them. But mostly we don’t really…

Vimeo CDN Locations

There is no turning back. Video streaming has become such a huge part of how we consume and approach media today. It’s not only become a medium — it’s turned into a language. Brands and entrepreneurs…

9 Quotes By Gandhi That Will Bring Out The Best In You

Mahatma Gandhi is one of the most known and influential teachers of the last century. He counts as one of our history’s most transformative and inspirational figures, and his work in order to support…