Python - Basics of Python list.
Python - Introduction to list.
- List is a sequence of elements or values. In an ordered way. The element can be of any type like integer, float, string.
- We can declare a list in python using square brackets [] or by using list() constructor.
- List is mutable - means we can change or modify the elements.
- List allows duplicate elements.
- List are Heterogeneous.
Questions asked in interview.
1- How to create an empty list?
Ans - list_object = list()
2 - Create a list with integer elements?
Ans :- list_object = list([1, 2, 3])
3 - Create a list using range function() ?
Ans :- list_object = list(range(3))
4- Why list is mutable object?
Ans - After defining or creating a list. We can accessed or change the element of a list in place.
Note : - list() can take Iterable as an argument for e.g :- range, string, set, tuple, dictionary.
In, the above question we create a list using constructor of a list class. What if interviewer asked to create list without using constructor. Simply, used [] separated by comma.
Example :- list_object = [1, 2, 3] or list_object = ["a", "b", 1, 1.4].
Accessing the value or element of the list.
- List element are accessible.
- We can access the list of the element using index.
- Index is always start from 0.
- The index of the last element in negative indexing is always -1.
Example:- l = [1, 2, 3, 4, 5, 6]
Forward Indexing = 0, 1, 2, 3, 4, 5
Syntax - list_variable = [index]
Questions asked in interview.
1 - How to access first element of the list.
Ans :- Taking above example - l[0] # it will print the first element of the list.
2- Is negative indexing is possible in the list.
Ans :- Yes, If we have large element in our list, so for knowing the index of last element we can use the negative indexing.
List Slicing
The slicing operator returns the subset of the list.
Few rules to consider in it.
- Syntax - variable_name[start_index : End_index, Step_Size].
- End index will always be -1.
- by default start_index = 0 and Step_size = 1
- Always slicing done from left element to right element.
- If step value is positive, move in forward direction.
- If step value is negative, move in backward direction.
int_lst = [1, 2, 3, 4, 5, 6, 7]
print highlighted part only (Will use list slicing in it).
How to approach on this type of problem.
- Think about the start index, by default start_index is 0.
- Now, think about what would be the End index i.e., if you are choosing end index is 5 it will be only run till 5 - 1 = 4.
- Last and not least think about the step size which would play a vital role in solving the complex examples of list slicing.
Ans - int_lst = [2 : 5]
Questions asked in interview.
Q- How to reverse the list?
Ans - Reverse the list [ : : -1]
Q - Print whole list?
Ans - list_variable [:]
Q - Evaluate following statements: (Don't see the answer firstly try to do by your own)
l = [1, 2, 3, 4, 5, 6]
- l[:]
- l[0:4]
- l[:-1]
- l[-1:]
- l[-1]
- l[::-1]
- l[:-1:]
- l[:-2:]
Important list functions.
Function used to update the list.
- Append() - Insert the single element at the last of the list.
- Extend() - Insert the multiple element at the last of the list.
- Insert() - Insert a particular element at a given index.
Function used to Delete the element of a list.
- Remove() - Remove particular element from the list.
- Pop() - Remove last element from the list if no index is given.
- Del() - Used to del(delete) the element from a list.
Function used to Sort the element of a list.
- Sort() - Sort the list element.
- Sorted() - It will take list as an argument.
Function to know the index of the value in a list.
- Index()- Returns the index of first matched item from the list. If given element is not in the list generates the value error.
Function used to count the number of time specified value appear in a list.
- Count() - Return number of times specified value appear in a list.
Function used to Reverse the element of a list.
- Reverse() - Reverse the element of the list in place.
Function used to Empty list.
- Clear() - To remove every element from the list.
Built-In Function
- Len() - Returns the total number of elements in a list.
- Sum() - Returns the sum of all the elements in a list.
- Min() - Returns the minimum elements in a list.
- Max() - Returns the maximum elements in a list.
Important list operator.
- We can only add two lists. Using + operator. (We can't add list with list or list with string).
- We can only multiply or replicate the element of a list using * operator. (We can't multiply list with list of list with string).
- We can determine whether an element is a part of the list or not using in operator. It's returns True if element is present and False if the element is not present in the list.
- We can determine whether two variable refers to the same object or not using the is operator. Example li = [1, 2, 3] and li1 = [1, 2, 3] now check li is li1 (checking if two list variable refer to the same object. You will get the output as false because in case of a list if two variables contains the list with the same number of elements then both the variables refers to two different objects. Means, python will create a fresh memory location for li2.
Questions asked in interview.
1 - If two variables are identical does it means that they are having same number of elements?
Ans - Yes.
2- If two variable are equivalent does it means that they both belong to same object or they both are identical.
Ans - No.
That's all the basic of python list.
Advance topic include.
- List Comprehension.
- Del Operator (Use).
- List and Strings (Converting string to list).
- Passing list to a function.
- Do programming on a list.
Comments
Post a Comment