List in python programming | Complete Python Tutorial |TechWithCode.com

Before knowing about List in python,We should know about where the List comes from.
so
     List is a collection datatype in python.
List in python programming | Complete Python Tutorial |TechWithCode.com



What is Python Collections?

If you know some other programming language then you are familiar with Array. Python Collections is similar to an array.

There are four collection data types in the Python programming language:

  • List is a collection which is ordered and changeable. It allows duplicate members.
  • Tuple is a collection which is ordered and unchangeable.It also Allows duplicate members.
  • Set is a collection which is unordered and unindexed. It does not duplicate members.
  • Dictionary is a collection which is unordered, changeable and indexed. It does not duplicate members..

Python List :-

                                   List is a collection which is ordered and changeable. It allows duplicate members. In Python ,lists items are written with in [ ] square brackets.

Sample Code For How Python List are created:-

In the following code, I have made a list named as lst1.It contains  21,32,4,6,12,2,1  integer value


Same as above code, I have made a list named as lst2 In the following code, which contains some string



Now I am combining the above both code and run it and show the output to you



output of the above python list code:-



How to access the Item of the List in python:-

1.Accessing by the index number

         You can access the list items by referring to the index number.
         Syntax:            Listname[index number]
         For more clarification see the code given below...

2.Accessing by the Negative index number

           You can access the list items by Negative indexing  which means beginning from the end,
                      -1 refers to the last item,
                      -2 refers to the second last item etc.

3.Accessing by the Range of index number

            You can access the list items by Range of the index.
             syntax:
                             Listname[ staring index value : ending index value]

              Note:      It includes the starting index and excludes the ending index
                             you may leave out the starting index, it will display 0 to ending index value
                             you may leave out the ending index, it will display starting index to last value

             example:  list1[2;6]
                                             It will give value at index 2 to 5
                                             It include the starting index and exclude the ending index

Sample Code For How to access the item in Python List:-



output of the above python list code:-


How to Change Item Value in Python list:-

In the starting, I have told you that List is changeable so let's see how can change or manipulate the list item. There are many functions in python through which we can manipulate it.

syntex:-                     listname[index]=new value

Example:-                lst[2] = 9999
                                                       it will change the value of index 2 in the list lst

Sample Code For How to change the item value in Python List:-




output of the above python list code:-





How to Add Item in Python list:-

        1. To Add item at last index of the List, We use append() method.
                            Syntax:-         Listname.append(item)

        2. To add an item at a specific index, we use insert() method
                           Syntax:-          Listname.insert(intex_number, item)
                                                                                  

        Note:- Always remember that the index of List starts from 0.

Sample Code For How to Add item value in Python List:-

output of the above python list code:-




How to Remove/Delete Item from Python list:-

1. To Remove the Specific item from List, We use remove() method.
                            Syntax:-         Listname.remove(item)

           Note: If item is not present in the list it will give you an error like[ ValueError:                                         list.remove(x): x not in list]

2. To Remove an item at a specific index, we use pop() method
                           Syntax:-          Listname.pop(intex_number)   OR   Listname.pop()

              If you do not give index_number then it will remove the last item of the List

3. To Remove an item at a specific index, we can also use del  keyword
                           Syntax:-         del Listname[intex_number]

4. To Delete All the item from the list, we  use clear() Function
                           Syntax:-          Listname.clear()

5. You can also delete the list completely by using del keyword.
                           Syntax:-         del Listname                                              
                   But before deleting the list make a note that you will never gong to access that list                               anymore


Sample Code For How to Remove/Delete item value from Python List:-


output of the above python list code:-


How to Copy the List in Python:-

In order to write code, you may need to copy the whole List. so there are some method which helps us in this because You cannot copy a list simply by typing list2 = list1.

1. To  copy the list in python, we use copy() Method
                                    Syntax:-        new_List = old_List.Copy()
1. To  copy the list in python,we also use list() Method
                                    Syntax:-        new_List = list(old_List)

Sample Code For copy  List in Python :-

output of the above python list code:-



How to Join Two List in Python:-

There are many ways to join two List in the Python programming language. Some easy and important methods are given below.
1.By using + Operator 
                         Syntax:-               List_name3 = List_name1 + List_name2
2.By using  extand() Method
                         Syntax:-              List_name1.extends( List_name2)

        NOTE:- Final result will be stored in list_name1

Sample Code For copy  List in Python:-

output of the above python list code:-



Python List Methods

Python has some built-in methods can be used on Lists.

Method                          Description

append()     ------------->      Adds an element at the end of the list


clear()     ------------->     Removes all the elements from the list


copy()        ------------->     Returns a copy of the list


count()   -------------> Returns the number of elements 


extend()   -------------> Add the elements of a list, to the end of the current list


index()   -------------> Returns the index of the first element 


insert()  -------------> Adds an element at the specified position


pop()   -------------> Removes the element at the specified position


remove()   -------------> Removes the item with the specified value


reverse()   -------------> Reverses the order of the list


sort()        ------------->  Sorts the list

If This post helps you a little bit then please share this post with your friends and like our page on social media link is given below.For more Computer and programming Related Tips and tricks please subscribe Our youtube channel clickhere

Don't forget to share how you like this Content in the comment below. If you have any doubt or suggestion then please write in the comment section below, we will love to hear from you.

                               thank you.

                                        ( नमस्ते )

                                     🙏   


No comments:

Powered by Blogger.