list in python

What is list in Python ?

A list in Python is an ordered and mutable collection of items. It is created using square brackets and separating items with commas. Python lists can store any type of data, including numbers, strings, and even other lists. list operations in python with examples 

  1. list elements are ordered and changeable and allow duplicate elements.
  2. list elements are indexed, the first elements are indexed [0] and second [1] and so on. 
  3. We can create list by using square brackets[]

1). Create a list in python:

        mylist = ["Apple","Banana","Graphs"] 
        mylist = ["Apple",2,True,0.5] 

Note : Any datatype of element we put in list.

2). Find type of any variable:

        mylist = ["Apple","Graphs","Banana"]
        print(type(mylist))  

Output is  <class 'list'> 

3). Accessing and Slicing a Python List:

You can access and manipulate items in a Python list using their index. The index of a list starts from zero, which means that the first item in the list has an index of 0, the second item has an index of 1, and so on. You can access a specific item in a list by using its index in square brackets

First Example:

        mylist = ["Apple","Graphs","Banana"]
        print(mylist[1])

Output is Graphs 

Second Example:

        mylist = ["Apple","Graphs","Banana","Kiwi","Charry"]
        print(mylist[1:4])

Output is ['Graphs', 'Banana', 'Kiwi']

4). Merge Two lists:

Merging two lists is a common task in Python programming. There are several ways to merge two lists in Python, and in this article, we will explore some of the most common methods.

First Method:

Using the + operator The simplest way to merge two lists is to use the + operator. This operator combines two lists into a new list that contains all the items from both lists.

For Example:

        fruits  = ["apple","banana","mango"] 
        animals = ["tiger","cats","dog"]
        new = fruits+ animals
        print(new)

Output is  ['apple', 'banana', 'mango', 'tiger', 'cats', 'dog']

Second Method: 

Using the extend() method Another way to merge two lists is to use the extend() method. This method adds all the items from one list to the end of another list.

For Example:

        fruits  = ["apple","banana","mango"] 
        animals = ["tiger","cats","dog"]
        fruits.extend(animals)
        print(fruits)

Output is ['apple', 'banana', 'mango', 'tiger', 'cats', 'dog']

Third Method:

We can also merge two lists by using for loop. This method is very useful when you need to merge two lists while performing some sort of manipulation or filtering on the items in the lists.

        fruits  = ["apple","banana","mango"] 
        animals = ["tiger","cats","dog"] 
        for x in animals: 
        fruits.append(x) 
        print(fruits)

Output is ['apple', 'banana', 'mango', 'tiger', 'cats', 'dog']

5). List Operations:

i). Adding element in list:

We have two method to adding element in list.

First Method:

Adding element in list by using append() functions. In this method add element/items end of the list only.

        mylist = ["Apple","Graphs","Banana"]
        mylist.append("Coconut")
        print(mylist) 

Output is ['Apple','Graphs','Banana','Coconut'] 

Second Method:

Adding element in list by using insert() functions. In this method we add element/items in any place of list like middle, end, start etc. insert(index number, item)

        mylist = ["Apple","Graphs","Banana"]
        mylist.insert(1,"Coconut")
        print(mylist) 

Output is ['Apple','Coconut','Graphs','Banana'] 

Note: Adding element only on end of the list

ii). Element Updating:

In Python, you can update an element in a list by assigning a new value to the element at a specific index.

        mylist = ["Apple","Graphs","Banana"]
        fruits[1]= "Coconut" 
        print(fruits)

Output is  ['Apple','Coconut','Banana']

iii). Remove Element:

We have two method for remove elements in list.

First Method:

Remove element in list by using pop() functions. In this method you can remove element/item from list at the end of list only.

        mylist = ["Apple","Graphs","Banana"]
        mylist.pop()
        print(mylist) 

Output is ['Apple','Graphs'] 

Second Method:

Remove element in list by using remove() functions. In this method you can remove element/items in any place of list like middle, end, start you provide only element name which you want to remove from list. remove(element name)

        mylist = ["Apple","Graphs","Banana"]
        mylist.remove("Graphs")
        print(mylist) 

Output is ['Apple','Banana'] 

iv). Reverse List:

If you want to reverse your list so you have two methods to reverse your list and those methods are given below.

First Method:

Reverse element in list by using reverse() functions. In this method no matter which your list contain same datatype elements or not.

For Example:

        mylist = ["Apple","Graphs","Banana",3,4,True]
        mylist.reverse()
        print(mylist) 

Output is [True,4,3,'Banana','Graphs','Apple'] 

Second Method:

Reverse element in list by using sort() functions. In this method dependent on same datatype elements list contains. It is too important your list have same datatype

For Example:

        mylist = ["Apple","Graphs","Banana"]
        mylist.sort(reverse=True)
        print(mylist) 

Output is ['Banana','Graphs','Apple'] 

LIKE | SHARE AND SUBSCRIBE ðŸ‘ˆðŸ‘ˆðŸ‘ˆ

Some Videos for better understanding ðŸ‘‡ðŸ‘‡ðŸ‘‡