If you are learning the Python language, then you should learn a popular data structure in the AI world called Lists.
In real-world applications, it is mostly used in two ways:
a) Storing data into an AI model
b) Managing user input
What is a List in Python?
It is an ordered collection of items that can hold different data types like numbers, strings, or even other lists.
In simple words, you can understand it like a shopping list where you can add, remove, or rearrange items anytime.
Example:
Please note that lists are defined using square brackets [].
Accessing Elements in a List
In Python, if you want to access an element, then you can access it by using its index position.
Indexing always starts from 0.
Example 1:
Example 2:
Real-World Application:
1. It is used to access a specific data from a dataset. For example, you can use it to access the first element from a CSV file.
2. In game programming, it is used to get the particular position of a player.
Why it matters:
It is used in many real-world projects like sorting, filtering, and searching.
How to Change a List (Common Modifying Methods):
1. append() method:
It is used to add a new element at the end of a list.
Example :
Real-World Application:
1. It is used to collect logs in an application.
2. It is also used to add a user’s input to a list.
Why it matters:
You can grow your list in a dynamic way using the append() method. That is why it is used in every automation script.
2. remove() method:
It is used to remove the first matching element from the list.
Example:
Real-World Application:
1. It is used to remove an added item from your shopping cart list.
2. It is also used to delete an already booked seat from a list of available seats.
Why it matters:
It is an easy way to remove an unwanted or duplicate item from a list.
3. insert() method:
It is used to add an element at a specific position in a list.
Example:
Real-World Application:
1. In a sorted list, it is used to add a missing data entry.
2. You can use it to insert a new event in a schedule.