Python Data Structures 101: 1. How to LIST??

In this post, I'm going to talk about the list, one of the most common but powerful data structures in Python. Let's get started.

Q1. What is a python list?
 Ans: List is a part of core Python Language. It is implemented as a dynamic array. It is heterogeneous(can hold elements of different data types) and is mutable unlike string and tuples in Python.


Q2. How to declare a list?
Ans: We can use list() to make a list
l = list()
Alternatively, we can use
l = []
Just to be sure, we can view the type of the object by writing
print(type(l))



Q3. How to add items in a list?
Ans: 
  • Use append() method: 
    l.append(1) 
    This adds 1 to the list.
  • Use insert() method: 
    # Using insert we can insert an element at particular index
    # But this increments the size of list by 1 by shifting all the elements towards right
    l = [1, 2, 3, 4, 5]
    l.insert(2, 0)
    print(l)

Q4.  How to find the length of the list?

length_of_list = len(l)

Q5. How to combine two lists?
A = [1, 2, 3, 4, 5]
B = [6, 7, 8, 9]
  • Non Pythonic Way
    combined_list = list()
    for a in A:
        combined_list.append(a)
        
    for b in B:
        combined_list.append(b)
    print(combined_list)
    
  • Using extend()
    # extend() is inplace i.e modifies the original list 
    A.extend(B)
    print(A)
    
  • Using + operator
    combined_list = A + B
    print(combined_list)
    
Q6. How to modify item in a list? Suppose

l = [1, 2, 3, 4, 5]
and we want to set element at index 2 = modified"?
l[2] = "modified"


Q7.  How to remove item from a list?
Ans: 
  • Using pop() method
  • # pop() removes the last elemeent from the list
    popped_item = l.pop()
    print(popped_item)
    #Trying to pop from an empty list throws an IndexError
    empty_list = []
    empty_list.pop()
    
  • Using pop() method to delete element at particular index
  • # We can use pop() method with arguments to delete element at particular index
    l = [1, 2, 3, 4]
    # Delete element at index 2
    l.pop(2)
    # Note that pop returns the value of deleted element
  • Using del
  • l = [1, 2, 3, 2, 3]
    # deletes element ar index 2
    del l[2]
    # We can use slicing to delete elements
    l = [1, 2, 3, 2, 3]
    # Deletes elements from index 2 till the end of the list
    del l[2: ]
  • Using remove() method to delete all element by value
  • # remove() remove the element by value
    l = [1, 2, 3, 2, 2]
    # removes 2 from the list
    # Note that there are multiple occurence of 2 in the list
    # remove deletes the first occurence of the 2
    print("Before Removing {}".format(l))
    l.remove(2)
    print("After removing {}".format(l))
    
    
Q8. How to reverse the list?
Ans:
  • using reverse() method:
  • # reverse() method reverses the list
    # It is an inplace operation i.e the original list is modified
    l = [1, 2, 3]
    l.reverse()
    print(l)
  • using reversed function:
  • # reversed function returns an reverse iterator to access the list
    
    l = [1, 2, 3]
    print(type(reversed(l)))
    
    # TO obtain the reverse list we must first obtain the list from iterator using list(reversed(l)) and assign it to some variable
    reversed_list = list(reversed(l))
    print(reversed_list)
  • Using slicing:
  • l  = [1, 2, 3]
    reversed_list = l[: :-1]
    print(reversed_list)
Q9. How to find index of particular element?
Ans:
# index() returns the index of irst foccurence of  the element
l = [1, 2, 2, 3, 4, 5]
l.index(2)
Q10. How to count number of occurence of element in list?
Ans:
l = [1, 2, 2, 4, 3, 2, 6]
l.count(2)

 

Q11. How to clear the list?
ANS:
l = [1, 2, 2]
l.clear()
l = [1, 2, 3]
l = []


Q12. How to copy list?
A = [1 ,2 ,3]
B = A[:]
A = [1, 2, 3]
B = A.copy()
Note: Do not use the below snippet to copy as the memory location is copied instead of actually creating copies of values.
A = [1, 2, 3]
B = A
What if we do?
B[0] = "1"
Does it change the contents of only B or both A and B?


Q13. How to see what methods are available in list?
Ans: 
print(dir(list())
Note: You can use the dir() function for any objects to see what's available

     
Q14. How to make a list of size n initialized with 0?
Ans:
l = [0] * n 

Q15. How to make a list of size m * n initialized with 0?
Ans:
l = [[0] * n] * m 




Share:

0 comments:

Post a Comment