Python list
In this article, we will be discussing one of the essential topic of Python programming, i.e., Python List.
1.Is a list mutable?
YES, list is mutable because we can modify the list once we created.
2.Does a list need to be homogeneous?
NO, There is no need to be homogeneous because .List is used to store the collection of values. List can be either homogeneous and heterogeneous.
3.What is the difference between a list and a tuple.
The main difference between a list and a tuple is that List are mutable whereas a tuple is immutable that means list can be changed but tuples can't change once it created.
4.How to find the number of elements in the list?
We can find the number of elements in the list is to use the Python built-in function len().
5.How to check whether the list is empty or not?
We can check whether the list is empty or not .we can use len() function .It returns number of elements in a sequence. If len() returns zero, the list is empty.
6.How to find the first and last element of the list?
Using index. In any list the first element is assigned index value 0 and the last element can be considered as a value -1.
7.How to find the largest and lowest value in the list?
We can use predefined function max() to find the largest element in a list and min().
8.How to access elements of the list?
To access values in lists, use the square brackets for slicing along with the index or indices to obtain value available at that index.
9.Remove elements in a list before a specific index.
We can use pop() to remove elements in a list before a specific index.
10.Remove elements in a list between 2 indices.
11.Return every 2nd element in a list between 2 indices.
12.Get the first element from each nested list in a list.
13.How to modify elements of the list?
To change a list element we use, list name followed by the index position inside the square brackets ([]), and then provides the new value using the Equal sign.
14.How to concatenate two lists?
we can concatenate two list using different methods of python.
Using *operator:
15.How to add two lists element-wise in python?
16.Difference between del and clear?
In Python dictionary, del keyword is used to delete a particular element. The clear( ) function is used to delete all the elements in a dictionary.
17.Difference between remove and pop?
The difference between remove and pop is that remove() delete the matching element whereas pop removes the element at a specific index. del and pop deals with the index.
18.Difference between append and extend?
The difference can define as append() adds a single element to the end of the list while . extend() can add multiple individual elements to the end of the list. append() takes a single element as argument while extend() takes a multiple element as argument.
19.Difference between indexing and Slicing?
Indexing means referring to an element of an iterable by its position within the iterable. Slicing means getting a subset of elements from an iterable based on their indices.
20.Difference between sort and sorted?
sort:
sort() function is very similar to sorted() but unlike sorted it returns nothing and makes changes to the original sequence. Moreover, sort() is a method of list class and can only be used with lists.
sorted:
sorted() method sorts the given sequence either in ascending order or in descending order and always return the a sorted list. This method does not effect the original sequence.
21.Difference between reverse and reversed?
The reverse() function in python doesn’t take any argument and reverse() method does not return any value in python but reverse the given object from the list whereas reversed() returns an iterator that accesses the given sequence in the reverse order.
22.Difference between copy and deepcopy?
The major difference between copy and deepcopy is that generally we use = operator to create a copy of an object and it doesn't create new object. It only creates a new variable while Deepcopy creates a new object and recursively adds the copies of object present in the original elements.
23.How to remove duplicate elements in the list?
We can remove duplicate elements in the list by using set of data structure.
24.How to find an index of an element in the python list?
The index() method returns the index of the specified element in the list.
25.How to find the occurrences of an element in the python list?
Count() method helps to find the occurrences of an element in the python list.
26.How to insert an item at a given position?
insert() is an inbuilt function in Python that inserts a given element at a given index in a list.
27.How to check if an item is in the list?
Python in is the most conventional way to check if an element exists in list or not. This particular way returns True if element exists in list and False if the element does not exists in list. List need not be sorted to practice this approach of checking.
28.How to flatten a list in python?
The process of flattening can be performed using nested for loops, list comprehensions, recursion, built-in functions or by importing libraries in Python depending on the regularity and depth of the nested lists.
29.How to convert python list to other data structures like set, tuple, dictionary?
We can use Typecasting to convert python list to tuple: tuple can be done by simply using tuple( list_name).To convert a list to dictionary, we can use list comprehension and to convert python list to set we can use python set() function to convert list to set. It is simplest way to convert list to set.
30.How to apply a function to all items in the list?
Using map() method we can apply a function to all items in the list. It takes two arguments: "iterables" and "functions " and return a map object.
31.How to filter the elements based on a function in a python list?
The filter function takes in two arguments: the function that checks for a specific condition and the sequence we want to apply it to and filter function takes each element from our list and passes it in to the function we give it. If the function with that specific element as an argument returns True, the filter function will add that value to the filter object . If the function returns False, then that element will not be added to our filter object.
32.How python lists are stored in memory?
The list is the fundamental linked data structure, whereas the array is the fundamental sequentially-allocated data structure. Lists are stored in distinct chunks of memory which are linked together with pointers, which enables efficient use of memory generally and doesn't require resizing.
Lucknow
3 年????
Software Engineer @Daxko | Microsoft Stack
3 年Nice article
Python | ODOO | HTML | CSS | Bootstrap | SQL | MySQL
3 年????