List in Python

In this tutorial we will learn about List in Python. List is similar to arrays expect one difference List can contain different type of data(Integer, Strings and Object) in single list. In list we add list element according to index number. First element of list would always save on 0 index. Indexing of list is same like indexing of array. It mean each element of list will have its own index number.

List in Python


Initialization of List in Python:

List in Python can be initialize using square brackets []. We can place our list elements in this square brackets. List in Python doesn’t need a built-in function for creation of list. Check this example to understand it.

Example

# declaring list
first_list = []
# simple list
first_list = [10, 15, 20]
# list with mixed datatypes
first_list = [10, "owlbuddy",5.5]



Nested List in Python:

When add a List as a element in another list it is known as Nested List. Here we will see example of Nested List.

Example

# nested list
nested_list = [2,[1,2,3],5]



Access elements from a list:

Now it's time to learn how we can access elements of List. There are various ways to access List elements like using List Index, using Negative Indexing etc. we will learn about each way in detail.

List Index:

We can access elements of list using List index. List index would always Integer. List index starts from 0 and each element have its own index. For example if a List have 10 elements it mean index of List will from 0 to 9.

Example

#accessing elements of list
first_list = ['O','w','l','b','u','d','d','y']
# Output: O
print(first_list[0])
# Output: b
print(first_list[3])



Negative indexing:

In Python we can access elements of List using negative indexing. It mean last index of List would have index -1 and second last index of List would have index -2. Check this Illustration to understand it more clearly.

List in Python
Example

#accessing elements of list
first_list = ['O','w','l','b','u','d','d','y']
# Output: y
print(first_list[-1])
# Output: u
print(first_list[-4])



Adding Elements into Python List:

There are various ways to add elements in List like append() method, insert() method, extend() method. We will learn about each of method. Let's start with append.

Adding elements in List using append() method:

We normally is append() method to add elements in List. With the help of append() method we can add a single element at a time in List. We can also add Tuples to List using this append() method. Check this example of append() method.

Example

#accessing elements of list
first_list = []
# adding elements in List using append() method.
first_list.append('O')
first_list.append('w')
first_list.append('l')
first_list.append('b')
first_list.append('u')
first_list.append('d')
first_list.append('d')
first_list.append('y')
# List after adding elements
print(first_list)


Adding elements in List using insert() method:

We use append() method to add element in List at the last position of List. But if you want to add Element in List at specific index you can use insert() method. Check this example to learn insert() method.

Example

#accessing elements of list
first_list = [1,5,8,4,2]
# adding elements in List using insert() method.
first_list.insert(0,5)
first_list.insert(2,6)
# List after adding elements
print(first_list)


Adding elements in List using extend() method:

The extend() method is use to add multiple elements at a single time in List. Check this example.

Example

#accessing elements of list
first_list = [1,5,8,4,2]
# adding elements in List using extend() method.
first_list.extend([15,14,18])
# List after adding elements
print(first_list)


Furthermore, we can also use + operator to add concatenate two Lists and we can use * operator to repeat List for multiple times. Check this example.

Example

# adding two List using + operator.
first_list = [1,5,8]
second_list=[5,3,7]
# List after adding elements
print(first_list+second_list)
# Use of * Operator to show List multiple times
print(first_list * 2 )



Removing Elements from the List:

We can remove elements from List using various methods like remove() method, pop() method and del keyword. We can also use clear() method to clear while list at once. We will learn about each method in detail.

Deleting elements from List using remove() method:

We can use remove() method to delete a element from string. Check this example.

Example

first_list = [1,5,8,8,5,1,6,1,15,12,10,3,2]
# using remove method
first_list.remove(5)
# list after remove method
print(first_list)
# removing range of elements from List using remove method
for i in range(1, 3): 
    first_list.remove(i) 
# list after remove method
print(first_list)


Deleting elements from List using pop() method:

We can use pop() method to delete a element using index number from string. Check this example.

Example

first_list = [1,5,8,8,5,1,6,1,15,12,10,3,2]
# using pop method
first_list.pop(5)
first_list.pop(3)
# list after remove method
print(first_list)


Use of clear method in List:

We can use clear() method to remove all elements in List.

Example

first_list = [1,5,8,8,5,1,6,1,15,12,10,3,2]
# using clear method
first_list.clear();
# list after remove method
print(first_list)



Some methods in Python to use with list object.

Method Description
append() This method is use to add an element at the wnd of list.
extend() This method is use to add list into another list.
insert() This method is use to add an element in list at specific index.
remove() This method is use to remove an element in list.
pop() This method is use to remove an element from specific index in list.
clear() This method is use to remove all elements from list.
index() This method returns to first index of give value.
count() This method returns the count of number of items passed as an argument.
sort() This method use to sort a list in ascending order.
reverse() This method use to reverse order of elements in list.
copy() This method returns copy of list.


List in PythonClick Here to Download Jupyter Notebook of This Tutorial.


Share this tutorial with your friends on your social Media..

List in Python List in Python List in Python List in Python



Now You Can Follow All These Tutorials on Your Android Mobile Just Download This App.


Owlbuddy.com