In this Python tutorial, you will explore Lists in Python. By the end of this tutorial, you will have a comprehensive understanding of Lists.
Hello & Welcome!
Part 4- Python Tutorial: List in Python
In this Python tutorial, we will explore the following:-
- How to create a List?
- Split a string into a List?
- How to retrieve an item from a list?
- Slicing in a List?
- How to concatenate 2 lists?
- Update items on the List?
- How to append to a List?
- How to delete from the List?
- Creating a List inside a List.
How to create a List?
In Python, a list is defined using square brackets [], where all items are enclosed within the brackets and separated by commas (,).
One of the key advantages of a list is its flexibility—there are no size restrictions, and it can store items of different data types within the same list.
You can also create an empty list.
For Example-
#empty list
List = []
#list with integer values
List = [1,2,3]
#List with different Data type
List = [5, 7.9, "John"]
Split a string into a List?
This section was discussed in the previous tutorial on strings. Now, let’s review an example.
>>>List = "I am good man"
>>>List.split(' ')
OUTPUT-
['I','am','good','man']
How to access an item in a List?
List elements are indexed starting from 0 onward, just like strings. To access a specific item in a list, use the same method as you use for a string.
>>>List = [1,2,3,4,5,6]
>>>List[2]
OUTPUT-
3
>>>List[5]
OUTPUT-
6
Access element via Negative Indexing
In a list, the backward index begins at -1, followed by -2, and continues similarly, just like in a string. Let’s have a look-
>>>List = [1,2,3,4,5,6]
>>>List[-1]
OUTPUT-
6
Slicing in a List?
Slicing refers to extracting specific portions from a larger whole. We can use slicing to retrieve only a subset of items from a Python list, just like we do with strings in our previous tutorial.
Excited to learn how to perform slicing? Let’s get started!
>>>List = [1,2,3,4,5,6,7,8]
>>>List[0:4]
OUTPUT-
[1,2,3,4]
->It works by selecting elements from index 0 to index 3, excluding the element at index 4.
-> I will demonstrate how index numbers are assigned to each item.
>>>List = [1, 2, 3, 4, 5, 6, 7, 8] #Example of list with eight element in it
index= 0 1 2 3 4 5 6 7 #Each index number assign with respect to each element to the list
-> I hope you now have a clear understanding of list indexing.
How to concatenate 2 lists?
You can easily combine or merge two lists into one by using the ‘+’ operator. It is as simple as that. Let’s examine an example.
>>>List1 = [1,2,3,4,5]
>>>List2 = [6,7,8,9,0]
>>>List3 = List1 + List2
>>>List3
OUTPUT-
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
Update items on the List?
You can easily modify certain items in a list by specifying the index position of the item and replacing it with a new value.
Let’s see in the example-
>>>List=[1,2,3,4,5,6]
#change index 0 value with new value
>>>List[0]=9
>>>List
OUTPUT-
[9,2,3,4,5,6]
How to append to a List?
You can use the append () method to add a new item to your existing Python list. Simply pass the value to add as an argument to the append () method.
Let’s see in the example-
>>>List=[1,2,3,4,5,6]
#append element 7 in List[]
>>>List.append(7)
>>>List
OUTPUT-
[1,2,3,4,5,6,7]
How to delete from the List?
You can use the pop () method to remove specific items from your Python list. It automatically removes the last element from the list if no index is mentioned.
Let’s see in the example-
>>>List=[1,2,3,4,5,6]
# Use the pop() method to delete the last element in your list, which is 6 in this List[].
>>>List.pop()
>>>List
OUTPUT-
[1,2,3,4,5]
- But what if you need to remove specific items from your list, even if they are located in the middle? Don’t worry! Python provides a built-in method for this task: remove().
- You can use the remove() method to delete any item from a list, regardless of its position.
Let’s see in the example-
>>>List= [1,2,3,4,5,6,7]
#remove item 4 from your List
>>>List.remove(4)
>>>List
OUTPUT-
[1, 2, 3, 5, 6, 7]
- There is one more problem, Guys!… If your list contains two identical numbers and you attempt to remove both, the process will not work as expected.
- The remove() method deletes only a single occurrence of the specified item. Let’s look at an example where the list contains two identical numbers.
>>>List=[1,2,3,4,5,6,4]
>>>List.remove(4)
>>> List
OUTPUT-
[1, 2, 3, 5, 6, 4]
- It removes the first occurrence of item 4, starting from index 0.
->Guys!…. No need to worry! There’s another method to remove the item you want to delete, regardless of how many times it appears in the list.
-> Consider a list: List = [1, 2, 3, 4, 5, 6, 4, 8, 9]. If you want to remove the number 4 specifically at index 6 while keeping the 4 at index 3, you can use the del() method. This method allows you to delete an element at a specific index of your choice.
Let’s see in the example-
>>>List=[1,2,3,4,5,6,4,8,9]
>>>del(List[6])
>>>List
OUTPUT-
[1,2,3,4,5,6,8,9]
-> The syntax of the del() method is as follows: del (ListName[IndexPosition])
Creating a List inside a List.
If you want to create a list within another list, you can do so by passing the existing list name into the new list where you want to include it.
Confused?… Let’s look at an example to clarify this further.
#List 1
>>>Colors=['red','green','yellow']
#List 2
>>>Fruits=['apple','banana','cherry']
#Create a 3rd List with List 1 and List 2
>>>num= [Colors,Fruits,[1,2,3,4,5]]
>>>num
OUTPUT-
[['red', 'green', 'yellow'], ['apple', 'banana', 'cherry'], [1, 2, 3, 4, 5]]
- Here, Colors and Fruits are both lists. We also create another list named num. Within the num list, we include both the Colors and Fruits lists, along with additional elements of their own. This results in a single num list containing all these items.
- As demonstrated in the example, a list in Python can hold multiple data types. This is one of the key features of lists – it allows you to save different types of data within a single list.
-> To extract a specific list from the num list, use the following syntax:
>>>num= [Colors,Fruits,[1,2,3,4,5]]
>>>num[0]
OUTPUT-
['red', 'green', 'yellow'] #This is List Colors at index 0 from the num list
-> To retrieve a particular element from the list, use the following code:
>>>num= [Colors,Fruits,[1,2,3,4,5]]
>>>num[2][1]
OUTPUT-
2
- In num[2][1]:
[2] Indicates the index position of the list within a nested list structure.
[1] Specifies the index position of the element inside that particular list.
That concludes our discussion on Lists in Python. Hopefully, you now have a clearer understanding of how lists work!
Congratulations! You successfully learned the List in Python.
In the next tutorial, we will begin exploring Standard Input and Output in Python.
Enjoy Learning Python!