What are list in Python?
A list is a sequence of elements or an array placed in a square bracket '[]'.
A Python list can have mixture of data types, i.e. integers, floating point numbers, boolean values, strings... All can be put together into one list.
That means Python list is not data type specific.
Python list are mutable (it can be modified).
How to access the element of a Python list?
Index number's are used to access the element of a list.
Initialization of Python list
List in Python can be initialized by 2 ways-
- Using square bracket '[]'.
- Using 'list()' build-in function.
Initializing Python list using square brackets
Each element of the sequence is separated by comma and are placed inside the square bracket.
Program
Initialization of Python list using build-in 'list() ' function
For initialization of Python list using build-in 'list()' function, put the sequence in the square brackets and give this as an argument to the 'list()' function.
Program
Creating an empty Python list
Don't put any element into the square brackets and don't provide any argument to the build-in 'list()' function, to create an empty Python list.
Program
List operations in Python
Here you will get to know, what operations you can perform with the Python list.
Accessing an element
Use the index number of that element to access it.
Python list indexing start with 0, i.e. the first element will have '0' index number - second will have '1' index number - and soo on .
Program
Accessing with negative indexing
We can also access an element using a negative number.
Index number '-1' refers to the last element of the list, '-2' pointes to the second last element of the list and soo on.
This is used when you want to access the element from the last.
Program
List slicing in Python
Selection of particular range of elements in a list, has the following format
list_name[starting index: ending index]
Program
By not specifying the starting index, slicing will start from the first element.
Program
[0, 1, 2, 3, 4]
By not specifying the ending index, the elements will get selected till the end of the list.
Program
We can also specify the interval (steps)
list_name[starting index: ending index: steps]
Program
Range selection with negative indexing
Negative indexing is useful when you want to select the elements from the end of the list.
Program
Changing the element of a list
Program
>>> numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Length of a list
Use 'len()' built-in function to know the length of a list.
Program
Append/add element to the list
To add an element to a list use 'append()' method.
Which will add the element at the end of a list.
The element is passed as an argument to the 'append()' method.
Program
Python list insert() method
To add an element anywhere in a list, by specifying the index number.
The insert() method takes 2 arguments.
- Index
- Object/value
The object gets inserted at the index number we specify, and all other elements after it (elements at the right side) get shifted.
Program
Removing an element from a list
To remove an element from a list, there are more than one methods.
- Using remove() method.
- The element that has to be removed is passed as an argument to the remove() method.
- Using pop() method.
- The index number of an element (the element which we have to remove) is passed as an argument to the pop() method.
- If the index number is not specified then it will remove the last element of the list.
- Using del keyword.
- An element is removed by specifying the index of a list.
Clearing a list
The clear() method, makes the list empty.
Deleting a list
The del keyword will delete a list from the program.
Joining two or more list
Make use of '+' operator to join the list.
Program
Joining more lists...
Python list extend() method
Extend a list by appending elements form the sequence.
Program
List multiplication
The list gets repeated, to the number we specify.
Program
Copy a list
The command 'list1 = list2' will not work because, the changes made in list1 will automatically be made in list2. As both the lists will have the same memory location.
Program
Use the 'copy()' method to make a copy of a list.
Python list reverse() method
Reverse the order of a list.
Program
Python list count() method
Returns the number of occurrence of value in a list.
Program
Python list index() method
Return index of an element, in a list.
If an element is repeated, then the index of first repeated element is returned.
Program
Python list sort() method
Syntax / format :- sort(key=None, reverse=False).
The key and reverse are the keyword arguments.
When key is provided with a function, according to which sorting is done, by applying the function once to each list item . Default is set to None.
When reverse = False, then list is sorted in ascending order. And when reverse = True, then list is sorted in descending order. Default is False.
Program
Post a Comment
If you have any query, feel free to ask.