The dictionary meaning of LIST is a number of connected items or names written or printed consecutively, typically one below the other.
In real world, lists are considered in many ways like playlist of songs, collection of your favourite videos, a list of user database and there are many more. But, if you are given a task about handling a list of users and filter out specific users under specific conditions then it becomes a tedious job to filter the list by going through each and every user. In such scenarios, programming comes into place to make your work easier.
So, now let us understand 'What is a LIST?' in python programming. In python, the list is a mutable or changeable, ordered sequence of elements where each element or value that is inside of a list is called an item.
In string, how each value/element is represented as a character and similarly in list, the each value/element is separated by comma where each element can be a string, variable or constants enclosed in  bracket.
Syntax of List: ['LetsUpgrade', 'Batch1', B2, B3]
Nested List: ['LetsUpgrade', ['java', 'python', 'c++'], 1, 2, ['B1', 'B2'], 'Exam']
Topics to be covered in LIST are as follows:
- Indexing Lists
- Lists are Mutable
- Updating and Slicing Lists
- Use of operators in List
- Methods used in List
- Built-in functions in List
- Deleting Elements from List
- Constructing a List with List Items
- List and Strings
- Parsing lines
- Objects and values
- List arguments
- List Comprehensions
We are going to understand the above topics through Examples not by theoretical...
Each item in a list corresponds to an index number, which is an integer value, starting with the index number
For the list
COURSES, the index breakdown looks like this:
The first item, the string
'Java' starts at index
0, and the list ends at index
4 with the item
print(COURSES) OUTPUT R
If we call the list
COURSES with an index number of any that is greater than 4, it will be out of range as it will not be valid:
print(COURSES) Output: IndexError: list index out of range
The above one was Positive Indexing and now let's see Negative Indexing as shown below:
For the same list
COURSES, the negative index breakdown looks like this
print(COURSES[-3]) Output Python
#Lists are Mutable:
>>> numbers = [17, 123] >>> numbers = 5 >>> print(numbers) [17, 5]
Therefore, as per above example lists are changeable i.e mutable.
#Updating and Slicing Lists:
We can use indexing to change items within the list, by setting an index number equal to a different value. This gives us greater control over lists as we are able to modify and update the items that they contain.
If we want to change the string value of the item at index
'SQL', we can do so like this:
COURSES = 'SQL'
Now when we print
COURSES, the list will be different:
Similarly, it goes for Negative Indexing.
We can also call out a few items from the list. Let’s say we would like to just print the middle items of
COURSES, we can do so by creating a slice. With slices, we can call multiple values by creating a range of index numbers separated by a colon
print(COURSES[1:4]) Output ['SQL', 'Python', 'R']
When creating a slice, as in
[1:4], the first index number is where the slice starts (inclusive), and the second index number is where the slice ends (exclusive), which is why in our example above the items at position,
3 are the items that print out.
If we want to include either end of the list, we can omit one of the numbers in the
print(COURSES[:3]) Output ['Java', 'SQL', 'Python']
This printed the beginning of the list, stopping right before index
To include all the items at the end of a list, we would reverse the syntax:
We can also use negative index numbers when slicing lists, just like with positive index numbers:
One last parameter that we can use with slicing is called stride, which refers to how many items to move forward after the first item is retrieved from the list. So far, we have omitted the stride parameter, and Python defaults to the stride of 1, so that every item between two index numbers is retrieved.
The syntax for this construction is
z referring to stride. Let’s make a larger list, then slice it, and give the stride a value of 2:
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] print(numbers[1:11:2]) Output [1, 3, 5, 7, 9]
numbers[1:11:2] prints the values between index numbers inclusive of
1 and exclusive of
11, then the stride value of
2 tells the program to print out only every other item.
We can omit the first two parameters and use stride alone as a parameter with the syntax
print(numbers[::3]) Output [0, 3, 6, 9, 12]
By printing out the list
numbers with the stride set to
3, only every third item is printed:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
Slicing lists with both positive and negative index numbers and indicating stride provides us with the control to manipulate lists and receive the output we’re trying to achieve.