In this Python List article we are going to learn How to Work with Python List, so first of all what is List in Python ? A list is a collection which is ordered and changeable, or a list is a collection of items in a 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.
Python List Key Features
Python lists are one of the most commonly used data structures in Python. some of the key features of Python lists are:
- Ordered: Python lists are ordered, which means the items in the list are stored in particular order.
- Mutable: Lists are mutable, meaning they can be changed or modified after they are created. You can add, remove, or modify elements in a list.
- Dynamic: Lists are dynamic, meaning they can grow or shrink in size during runtime as elements are added or removed.
- Heterogeneous: Python lists can store elements of different data types, including integers, floats, strings, and even other lists.
- Indexable: Elements in a list can be accessed using their index, which starts from 0 for the first element and increases by 1 for each subsequent element.
- Slicing: Lists support slicing, which allows you to extract a portion of the list using the syntax list[start:stop:step].
- Built-in Functions: Python provides a range of built-in functions for working with lists, including len(), min(), max(), sum(), sorted(), and reversed().
- Versatile: Lists can be used to solve a wide range of problems, including storing and manipulating data, implementing algorithms, and building data structures.
Overall, Python lists are a flexible and versatile data structure that can be used for a wide range of tasks, making them an essential tool for Python developers.
Let’s create our first List in Python.
1 2 |
languages = ["Python", "Java", "C++", "C#"] print(languages) |
You can see that we have created a list of programming languages in Python, if you run the code
you will see the list output.
Accessing the Element in Python List
OK now we want to access specific element in a List, for accessing the items in python list you need to use the index of that element, and the index starts from 0, for example in here i want to access C++, so the index for C++ item is 2.
1 2 3 4 5 |
#our list languages = ["Python", "Java", "C++", "C#"] #accesing elements in print(languages[2]) |
Run the code and this is the result.
Using List Item with FString
So now we want to use fstring with list items.
1 2 3 4 5 6 |
#our list languages = ["Python", "Java", "C++", "C#"] text = f"I Love {languages[0]} Programming Language" print(text) |
Run the code this is the result.
Changing Item in a List
Also you can change an item in a list. so in here i want to change the first index in the list.
1 2 3 4 5 6 |
#our list languages = ["Python", "Java", "C++", "C#"] print("Orginal List", languages) languages[0] = "JavaScript" print("Changed List" , languages) |
This is the result
Adding Element to List
The simples way is using append, so when we append an item in list, the new item will be at the end of the list.
1 2 3 4 5 6 |
#our list languages = ["Python", "Java", "C++", "C#"] print("Original List", languages) languages.append("Kotlin") print("Added New Item ", languages) |
This is the result
Also using append you can create dynamically lists.
1 2 3 4 5 6 7 |
#our empty list fruits = [] fruits.append("Apple") fruits.append("Pear") fruits.append("Melon") print(fruits) |
Inserting Element to Python List
Using insert method you can add element 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 |
fruits.append("Apple") fruits.append("Pear") fruits.append("Melon") fruits.insert(0, "Banana") print(fruits) |
This is the result
Removing Element in Python List
There are different ways that you can remove items from a list, you can use del if you know
the position or index of the list.
- Removing with del keyword
1 2 3 4 5 6 7 8 9 10 11 |
#our empty list fruits = [] fruits.append("Apple") fruits.append("Pear") fruits.append("Melon") print("Original List", fruits) del fruits[1] print("Deleted List ", fruits) |
This is the result
- Removing with Pop() method
The pop() method removes the last item in a list, but it lets you work with that item
after removing it
1 2 3 4 5 |
languages = ["Python", "Java", "C++", "C#"] poped = languages.pop() print(languages) print(poped) |
This is the result.
- Removing an item by value
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("Removed Item",languages) |
This is the result.
Sorting List Permanently
For sorting the list permanently we can use sort method.
1 2 3 4 5 |
languages = ["Python", "Java", "C++", "C#"] print("Original List", languages) languages.sort() print("Sorted List", languages) |
This is the result
You can also sort this list in reverse alphabetical order. you can add reverse to True.
1 |
languages.sort(reverse=True) |
there is another function for soring lists that is called sorted(), using sorted() method
you can sort list items temporarily.
1 2 3 4 |
languages = ["Python", "Java", "C++", "C#"] print("Original List", languages) print(sorted(languages)) |
Subscribe and Get Free Video Courses & Articles in your Email