In this Python Tutorial we are going to learn about Python List, so first of all let’s talk about list, List is a collection which is ordered and changeable, or a List is a collection of items in particular order. You can make a list that includes the letters of the alphabet, the digits from 0–9, You can put anything you want into a list. you can use square brackets ([]) for creating list.
These are some key points to keep in mind about lists in Python:
- To create list, you can use square brackets to enclose the elements, separated by commas.
- Lists are indexed, meaning that you can access individual elements using their index starting from 0.
- Lists are mutable, which means that you can add, remove or modify elements after the list has been created.
- Lists can contain elements of any data type, including other lists.
- You can use built in functions and methods to perform various operations on lists such as sorting, reversing or adding elements.
Lists are very useful for storing and working with collections of data in flexible and dynamic way. they allows you to easily add or remove elements, and they provide different built in functionality for working with and manipulating lists. By understanding how lists work and how to use them effectively, you can take advantage of their strengths and improve the quality and efficiency of your Python code.
OK now let’s create our first Python List. this is a simple list of programming languages, and after that we want to print our list and also print the type of our languages.
1 2 3 |
languages = ["Python", "Java", "C++", "C#"] print(languages) print(type(languages)) |
Run the code and this is the result.
If you want to access an specific element in python list, than you need to use the index of that item, list indexing starts from 0.
1 2 3 |
languages = ["Python", "Java", "C++", "C#"] print(languages[3]) |
Now we are going to learn how you can use individual items from a list.
1 2 3 4 |
languages = ["Python", "Java", "C++", "C#"] text = f"I Love {languages[0]} Programming Language" print(text) |
As i have already said that Python List is changeable it means that you can change the items of the list, now let’s change an item in a list. in here we want to change the first index item in our list.
1 2 3 4 5 |
languages = ["Python", "Java", "C++", "C#"] print("Original List", languages) languages[0] = "JavaScript" print(languages) |
Run the code and this is the example.
Also you can add elements to a list, the simplest way is using append, so when we append an item in list, the new item will be added at the end of the list.
1 2 3 4 5 |
languages = ["Python", "Java", "C++", "C#"] print("Original List", languages) languages.append("Kotlin") print(languages) |
This is the result.
also using append you can create dynamically lists. so first we have created an empty list after that we are appending the items in to list.
1 2 3 4 5 6 |
fruits = [] fruits.append("Apple") fruits.append("Pear") fruits.append("Melon") print(fruits) |
using insert() method you can add elements at any position in your list, because when you are going to use insert you can specify the index of the element.
1 2 3 4 5 6 7 8 9 10 |
fruits = [] fruits.append("Apple") fruits.append("Pear") fruits.append("Melon") print("Original List",fruits) fruits.insert(0, "Banana") print(fruits) |
This is the result.
There are different ways that you can remove items from a list, you can use del keyword if you know the position or index of the list.
1 |
del fruits[1] |
The pop() method removes the last item in a list, but it lets you work with that item after removing it.
1 |
poped = languages.pop() |
For removing item by value we can use remove() method.
1 2 3 4 5 |
languages = ["Python", "Java", "C++", "C#"] print("Original List", languages) languages.remove("Java") print(languages) |
This is the result.
For sorting the list we can use sort() method.
1 2 3 4 5 |
languages = ["Python", "Java", "C++", "C#"] print("Original List", languages) languages.sort() print(languages) |
You can also sort this list in reverse alphabetical order by adding reverse to True.
1 2 |
languages.sort(reverse=True) print(languages) |
There is another function for soring lists that is called sorted(), using sorted() method you can sort list items temporarily.
1 |
print(sorted(languages)) |
Also you can reverse the order of a list.
1 2 3 4 5 |
languages = ["Python", "Java", "C++", "C#"] print("Original List", languages) languages.reverse() print(languages) |
Also you can find the length of a list.
1 2 3 |
languages = ["Python", "Java", "C++", "C#"] print(len(languages)) |