Read Python Interview Questions and Answers for Beginners and Experienced

Python List with examples

Python list is one of the Python collections type. Generally, a list is a collection of items. A list in Python also, a group of items. Here we will learn about list in Python, Various Python list Functions and Python list Operations.

Some points to be remember about Python List:

  • Elements in list are in ordered form, means it preserve the order in which the elements put into list.
  • A list can have duplicate elements.
  • A list element can be changeable. We can change the value of the elements after its creation.

 

Syntax of Python List

Basic syntax of Python list is

List_variable = [element 1, element 2, element 3… so on]

Observations:

  • List_variable is name of the python list.
  • element 1, element 2, element 3… so on are the elements of list.
  • Python list start and end with square brackets.

Example: Let’s take one example to understand Python list.

animal = ["cow","dog","cat","cow"]
print("type of animal is",type(animal))
print("elements in animal list are:", animal)

 

Output:

type of animal is <class 'list'>

elements in animal list are: ['cow', 'dog', 'cat', 'cow']

Explanation:  in above example, animal is the list, in which we have 4 animals as its elements. “cow” is duplicate element as in list we can have duplicate elements as discussed above.

 

Various Python list operations:

We have learnt how to create python list, now we will learn that what are various Python list operations we can perform on list in python. Below are the basic operations which we can perform on Python list.  we will also study various Python list functions under them.

  • Add elements into Python List
  • Remove elements from Python list
  • Update the element in Python list
  • Iterate the Python List.
  • Fetch the elements from Python list.
  • Sort the elements in list

 

Add elements in List

We will see how we can add new elements in list. There are multiple ways to add element into python list

Following are the ways

  • append(): This method will add the element to the end of the list.

Example:

animal = ["cow","dog","cat","cow"]

animal.append("pig")

print("type of animal is",type(animal))

print("elements in animal list are:", animal)

Output

type of animal is 

elements in animal list are: ['cow', 'dog', 'cat', 'cow', 'pig']

Explanation: “Pig” is added to the end of the list

  • index() : list.index(position, element_to_be_added) take two parameter. It will dd the element at the given position into the list.

Example:

animal = ["cow","dog","cat","cow"]

animal.insert(0,"pig")

print("type of animal is",type(animal))

print("elements in animal list are:", animal)

Output

type of animal is 

elements in animal list are: ['pig', 'cow', 'dog', 'cat', 'cow']

Here “pig” is added at 0th means first position in the list.

  • list.extend(): This method will add the list elements to another list.

Example:

animal1 = ["cow","dog","cat","cow"]

animal2=["horse"]

animal1.extend(animal2)

print("elements in animal1 list are:", animal1)

 

Output

elements in animal1 list are: ['cow', 'dog', 'cat', 'cow', 'horse']

Explanation: Here animal 1 and animal2 are two list. list.append() function in python will add the animal2 into animal1 list.

 

Remove elements from Python list

We can remove the elements from python list also. following ways we can use to remove the elements from the list

  1. remove : list.remove(element_to_be_remove) function will remove the first occurrence of the element in the list.

Example:

animal= ["dog","cow","cat","cow"]

animal.remove("cow")

print("elements in animal1 list are:", animal)

Output

 

elements in animal1 list are: ['dog', 'cat', 'cow']

Explanation: animal list has two “cow” elements. So, list.remove() function will remove the first occurrence of “cow”.

If we try to remove the element which is not present in the list, then list.remove() function will throw the error.

e.g.

animal= ["dog","cow","cat","cow"]

animal.remove("pig")

print("elements in animal1 list are:", animal)

Output

animal.remove("pig")

ValueError: list.remove(x): x not in list
  1. pop() : if we want to remove the element by its index number, then we can use list.pop(index_no_of_element) .

Example

 

animal= ["dog","cow","cat","cow"]

animal.pop(1)

print("elements in animal1 list are:", animal)

Output

elements in animal1 list are: ['dog', 'cat', 'cow']

Explanation: with list.pop(1), element “cow” was removed from animal list in python.

Update list element in python

 We can simply update the element by using index of list.

Example

animal= ["dog","cow","cat","cow"]

animal[0]="pig"

print("elements in animal1 list are:", animal)

Output

elements in animal1 list are: ['pig', 'cow', 'cat', 'cow']

 

Suppose we don’t know the index value of some element which we want to update, then we can use code something like this:

animal= ["dog","cow","cat","cow"]

index=0

for i in animal: 

    if i=="cat":

     animal[index]   ="pig"

    index=index+1

print("elements in animal list are:", animal)

Output:

elements in animal list are: ['dog', 'cow', 'pig', 'cow']

Explanation:

Here we don’t know the index value of “cat”, so we will use python for loop to update value of “pig” with “cow”.

 

Iterate the Python List

Iterate the python list means to traverse each and every element of list. In Python, we can traverse the list by following ways.

 

Traverse by for loop:

By using for loop we can traverse python list.

 

Example:

animals= ["dog","cow","cat","cow"]

for animal in animals: 

   print (animal)

Output

dog

cow

cat

cow

 

Traverse by while loop

Python list can also be traverse by while loop.

Example

 

animals= ["dog","cow","cat","cow"]

i=0

while i< len(animals):

    print(animals[i])

    i=i+1

 Output

dog

cow

cat

cow

 

Fetch the elements from Python list

Here we will learn that how we can fetch or retrieve the elements from python list. Fetching or accessing the elements from Python list is very simple. Below are some ways:

  • using index: Python list elements can be retrieve through index number

Example:


animals= ["Dog","Cow","Cat","Cow"]

print (animals[0])

Output


Dog

Explanation: we have animals list, when we access animals[0], it give us Dog, as Dog is present in the first position.

 

Sort the elements in Python list

Sort means to arrange the things in particular manner. In Python, we can sort the elements by using sort() method. Elements in Python list can be sort ascending or descending order.

Example:


numbers= [5,3,5,1,7,9]

#using sort method to sort the elements

numbers.sort()

print (numbers)

Output


[1, 3, 5, 5, 7, 9]

 

Various Python list methods or functions

Here we will study various python list methods or functions (will update soon…). Python list methods are described below:

append()
count()
clear()
extend()
copy()
index()
insert()
pop()
remove()
reverse()
sort()