In this tutorial, You will learn about Python List, the Difference between Python List, Tuple and Set, and Python list methods. In Python, a List is an object that contains a collection of items. The Python list provides various list methods that make it easy to work with the Python list. In the previous tutorial, we have seen all about the Python Pass statement.
In this guide, We will see What is a list in Python and also some important Python List Functions.
Headings of Contents
- 1 What is a Python list?
- 2 Data Type of List.
- 3 Access items from the Python List.
- 4 Slice of List in Python.
- 5 Negative Slicing:
- 6 Change value in List in Python:
- 7 Loop through a Python list:
- 8 Length of the list:
- 9 Add item to the list:
- 10 Remove item from the list:
- 11 del keyword use in Python list:
- 12 Python List Methods
- 13 List vs Set vs Tuple
- 14 Speed Comparison
- 15 Conclusion
What is a Python list?
The list is a collection data type in Python that stores collections of items that are Ordered, Changeable, or Mutable and Indexed. List in Python allows duplicate items which means you can assign duplicate elements inside the list at more than one place.
Python list is written in a square bracket ( [ ] ). Let’s create a Python list for better understanding.
myList = [1,2,4,5,6,7,7,8,9,9] print(myList)
Ordered
The list ordered means the Python list maintains the order if items items. List items have a defined order and that order can not be changed. When you will add a new item to the list, the new item will be added at the end of the list.
Changeable or Mutable
The list is changeable or mutable, Once you have created a list in Python, You can change it further, for example, add an item, remove an item, etc.
Duplicate Item
A list in Python allows duplicate items, meaning you can place the same item in more than one place in the list.
Data Type of List.
If you want to know the data type of the list, Then you will need to use type() built-in function.
myList = [1,2,4,5,6,7,7,8,9,9] print(type(myList))
Access items from the Python List.
To access the list item, You can use an index number which means using the index number you can access any element of the list.
The index starts at 0. The index number will be written in the square bracket ( [ ] ) as you can see below.
myList = [1,2,4,5,6,7,7,8,9,9]
print(myList[0])
Python supports negative indexing which means you can use a negative index number to access the list elements from the last of the list. The negative index starts from -1.
In myList list, the -1 index element is 9, and -2 index element is 9, and so on.
myList = [1,2,4,5,6,7,7,8,9,9]
print(myList[-1])
Slice of List in Python.
Sometimes you need to require some data from the list that lies between a specific range, Then you can use the slicing technique.
In Python slicing, you have to define the start and end index numbers for the result.
fruits = ['orange','banana','kiwi','mango','melon','apple','pineapple']
print(fruits[2:5])
The output will be: [‘kiwi’, ‘mango’, ‘melon’]
As you know in list slicing, the end index number element is not included in the result, for example in the above result, the apple index number is 5 not included in the result.
Negative Slicing:
You can also use negative slicing in the Python list the same as slicing. In negative slicing, you will need to define the negative index number instead of the positive index.
fruits = ['orange','banana','kiwi','mango','melon','apple','pineapple']
print(fruits[-5:-1])
The output will be: [‘kiwi’, ‘mango’, ‘melon’, ‘apple’]
Change value in List in Python:
Sometimes you will need to change specific items in the list, you can use the index numbers to change values.
fruits = ['orange','banana','kiwi','mango','melon','apple','pineapple']
fruits[0] = 'grapes'
print(fruits)
Loop through a Python list:
You can display the list of items using a loop. To display items from the list, use the for loop.
Example
ruits = ['orange','banana','kiwi','mango','melon','apple','pineapple']
for fruit in fruits:
print(fruit)
Output
orange
banana
kiwi
mango
melon
apple
pineapple
Length of the list:
To find the length of the list in Python you can use the len() built-in function.
fruits = ['orange','banana','kiwi','mango','melon','apple','pineapple']
print(len(fruits))
Add item to the list:
To add a new item to the list you need to use the append() Python list method.
fruits = ['orange','banana','kiwi','mango','melon','apple','pineapple']
fruits.append('Dates')
print(fruits)
If you want to add an item in the list on a specific index number then you can use the insert() Python method. insert method takes two parameters first is the index number and second is the item.
fruits = ['orange','banana','kiwi','mango','melon','apple','pineapple']
fruits.insert(2,'Blueberries')
print(fruits)
Remove item from the list:
a. To remove specific items from the list you need to use the remove() method.
fruits = ['orange','banana','kiwi','mango','melon','apple','pineapple']
fruits.remove('pineapple)
print(fruits
b. To remove a specific index item from the list, use the pop() method.
fruits = ['orange','banana','kiwi','mango','melon','apple','pineapple']
fruits.pop(6,'pineapple)
print(fruits)
Note:- if the index number is not specified then the pop() method removes the last index item.
del keyword use in Python list:
a. To delete specific items from the list, use the del keyword.
fruits = ['orange','banana','kiwi','mango','melon','apple','pineapple']
del fruits[0]
print(fruits)
b. If you want to delete the complete list, use the del keyword.
fruits = ['orange','banana','kiwi','mango','melon','apple','pineapple']
del fruits
print(fruits)
Python List Methods
Python List has a set of built-in methods that will be applied on top of the Python List always.
Python List Methods | Description |
---|---|
append() | The Python insert() method is used to add the element to the specific position. |
clear() | Python clear() method is used to clear the list. |
copy() | Python copy() method is used to copy the list. |
count() | Python count() return the total number of times, an element appears in the list. |
extend() | Python extends() method is used to add the elements of a list to the end current list. |
insert() | Python count() returns the total number of times, an element appears in the list. |
index() | Returns the index of the first element with the specified value. |
pop() | The Python sort() list method is used to sort the list. |
reverse() | Reverse the order of the list. |
sort() | The Python pop() method removes the element from a specific position. |
Now let’s see some important differences between Python List, Python Set, and Python Tuple.
List vs Set vs Tuple
These are the major differences between Python List, Tuple, and Set. Most of the time interviewer asked these differences that’s why it is important to know these differences as a developer.
Features | List | Set | Tuple |
Mutability or Changeable | Mutable | Mutable | Immutable |
Order | Ordered or Maintain Items Order | Unordered | Orders |
Allow Duplicates Items | Yes | No | Yes |
Indexing | Supports indexing | Does not Supports indexing | Supports indexing |
Does not support indexing | [1, 2, 3] | {1, 2, 3} | (1, 2, 3) |
Use Case | General-purpose storage | Unique item collections | Fixed collections |
Memory Usage | More memory overhead | Less memory due to immutability | Less memory per item |
Use in Data Structures | Used for dynamic collections | Used for fixed collections | Used for membership testing and eliminating duplicates |
Speed Comparison
Python List is slower than Python Tuple and Python Set because:
- Python lists can change in size (Add or Remove Items). These features of the Python list require extra overhead memory. These features make the list slow.
- When you check an item present in the list Python has to look one by one until it finds the item or reaches the end. This takes longer, especially for larger lists.
- Lists use more memory because they need to maintain information about their size and can store duplicates.
Why Tuple is Fast?
- Tuples cannot be changed after they are created. This simplicity allows Python to optimize memory usage and access speed, making it quicker to retrieve items.
- Since tuples are fixed in size and structure, they require less memory than lists. Less memory means faster access times.
- Tuples are designed for quick iteration. When looping through items, the overhead is minimal compared to lists.
Why Sets Are Fast?
- Sets use a hash table, which allows for membership checks (e.g.,
if item in set
). This is much faster than the linear search required for lists. - Since sets can only store unique items, thereβs no need to handle duplicates, making the underlying structure more efficient.
- Sets do not maintain any specific order, which simplifies their implementation and allows for faster operations.
Conclusion
Throughout this article, We have seen all about Python List and the Difference between Python List and other Python Data types Tuples, Sets, etc along with Python list methods. If you want to learn details about any list methods, Click on any listed method defined in the above table and learn them.
I hope you will like this post. If this post is helpful please visit and share it with your friends who want to learn Python programming from scratch to advanced.
Thanks for your valuable time, Happy Learning πππ