Table of Contents
Introduction What are Lists in Python? Create List Access a List Modify Lists Delete List Len() Function ConclusionIntroduction
Are you ready to dive into the exciting world of Python programming? One of the most fundamental data structures in Python is called a list. In this article, we will learn what lists are, explore how to make a list in Python, and perform some cool operations on it. Let’s get started!
What are Lists in Python?
Lists are an ordered collection of items that you can store and manipulate. Simply put, lists are used to store multiple items in a single variable. The list can be of any data type and can store anything you want.
Fun fact: Lists are one of 4 built-in data types in Python used to store collections of data. Now that you know what lists are, let’s discuss how to make these lists!
Create List
We can create a list by placing all the elements inside a square bracket [ ], separated by commas. In the code snippet below, we created a list called “my_list” that contains five numbers: 1, 2, 3, 4, and 5.
my_list = [1, 2, 3, 4, 5]
Did you know that you can also create a list that is empty initially and add items to it later? We use an inbuilt function for this task.
Append() Function
my_list = []
my_list.append(“apple”)
my_list.append(“kiwi”)
my_list.append(“orange”)
Here, we created an empty list called “my_list” and used the “append()” function to add three fruits to it. The append() function allows you to add items to the end of the list.
Access a List
Great job on creating a list! Now, let’s learn how to access its items. Python uses indexing to access individual items in a list. An easier way to understand this is to think that each item in a list is associated with a number. The index represents the position of an item in the list, starting from 0.
my_list = [“apple”, “kiwi”, “orange”]
print(my_list[0]) # Output: “apple”
print(my_list[1]) # Output: “kiwi”
print(my_list[2]) # Output: “orange”
In the above example, we printed the first, second, and third items in the my_list using their respective indices. Remember, the index starts from 0, so the first item has an index of 0, the second item has an index of 1, and so on.
Negative Indexing
You might be familiar with the ascending and descending numbering order. Similarly, in Python, we can assess a list from the beginning or the end. In Python, you can index any sequence with negative indices too. The index of -1 means the last item, -2 to the second last item and so on.
my_list = [“apple”, “kiwi”, “orange”]
print(my_list[-3]) # Output: “apple”
print(my_list[-2]) # Output: “kiwi”
print(my_list[-1]) # Output: “orange”
As you can see, we have now output all three fruits in our list using negative indexing.
Slicing Lists
You may sometime want to access a portion of a list. How do you think that can be achieved? Well, the simple answer is using the colon “:” For example:
# List slicing in Python
my_list = [“apple”, “kiwi”, “orange”, “pineapple” ]
# items from index 1 to index 3
print(my_list[1:4])
# items from index 2 to end
print(my_list[2:])
# items beginning to end
print(my_list[:])
What do you think the output would look like? You can compare your guess with the following:
[‘kiwi’, ‘orange’, ‘pineapple’]
[‘orange’, ‘pineapple’]
[‘apple’, ‘kiwi’, ‘orange’, ‘pineapple’]
Now that you know how to access a list, let’s discuss further how to modify one.
Modify Lists
You can modify or change Python lists easily, and there are several methods to do so. Are you ready to dive in further?
We have already discussed the “append()” function above. The next one is the “extend()” function.
Extend() Function
Learners, we can also use the extend() method to insert all the items of an iterable to the end of the list. For example:
num1 = [1, 2, 3]
num2 = [4, 5, 6]
# add elements of num2 to the num1list
num1.extend(num2)
print(num1)
The expected output of the above code snippet will be:
[1, 2, 3, 4, 5, 6]
Insert() Function
During your program, you may need to enter content at a specific index in the list. Luckily, the insert() method helps you add an element at the specified index.
numbers = [1, 3, 4]
# insert an element at index 1 (second position)
numbers.insert(1, 2)
print(numbers)
Can you guess what the output of the above code snippet will be?
[1, 2, 3, 4]
Change a List Item
Well, sometimes you may need to change an element of the list and replace it with some new content. Did you know that we can change items of a list by assigning new values using the = operator? Here’s how:
my_list = [“apple”, “kiwi”, “orange”]
my_list[0]= “banana”
print(my_list)
And the output of this code snippet will be:
[‘banana’, ‘kiwi’, ‘orange’]
Great job on learning this far. Now we will learn about how to delete an item from a list. Let’s continue!
Delete List
To delete an item from a list in Python, we will most commonly use 2 ways. Both of them have been discussed below.
Del() Statement
In Python, we can use the del statement to remove one or more items from a list. For example:
my_list = [“apple”, “kiwi”, “orange”]
del my_list[1]
print(my_list)
The following will be the output:
[‘apple’, ‘orange’]
Remove() Function
We can also use the remove() method to delete a list item. For example:
my_list = [“apple”, “kiwi”, “orange”]
my_list.remove(‘kiwi’)
print(my_list)
The following will be the output:
[‘apple’, ‘orange’]
Sometimes, you may need to know the length of your lists. In case the list is very large, then it can get tiring to count all the elements in a list. To do that, we have a Python function.
Len() Function
You can use this len() function to easily count the number of items in your list. For example:
my_list = [“apple”, “banana”, “orange”]
print(len(my_list)) # Output: 3
You have covered the most important creation, updation, modification, and deletion features of lists in Python. Kudos to your hard work and eagerness!
Conclusion
Congratulation! Today you have covered an important topic of Python programming. We know that after reading this article, you are excited to use Lists throughout your Python programming.
You can take the time to master the fundamentals of Python by taking our awesome Python courses. Let’s learn and program together!