Python List Tutorial

  • date 17th July, 2019 |
  • by Prwatech |
  • 0 Comments

 

Python List Tutorial with Examples

 

Python List Tutorial with Examples, In this tutorial, we will learn how to create a list in Python to your code and why it is important. Here, You will also learn various types of list methods in Python which are helpful for any Python developers. Are you looking for the Python list tutorial with examples or Are you dreaming to become to certified Pro Python Developer, then stop just dreaming, get your Python certification course from India’s Leading Python training institute.

 

Lists in Python referred to as a sequence that is an ordered collection of objects which can host all objects of any data type in Python. Lists are one of the most useful and versatile Python Data Types. Do you want to know types of list methods in Python and how to create a list in Python, then just follow the below mentioned Python list tutorial for Beginners from Prwatech and take advanced Python training like a Pro from today itself under 10+ years of hands-on experienced Professionals.

 

What is Python List?

 

List in python is ordered collection which is mutable in nature. In Python list is enclosed with square brackets where elements are separated using a delimiter ‘,’ (comma).

Ex)    L1= [11,22,33,44]

L2= [“a”, “aa”, “abb”]

 

How to create a list in Python?

 

List elements can be referred to using the index numbers where indexing starts from 0.

Ex)    L1= [11,22,33,44]

print(L1[2])

To change the value of a specific element, refer to its index number:

Ex)    L1= [11,22,33,44]

L1[2] = 55

print(L1)

You can loop through list elements by using a for loop:

Ex)    L1= [11,22,33,44]

for x in L1:

print(L1)

To determine if a specific element is present in a list use keyword ‘in’.

Ex)    L1= [“a”, “ab”, “aaa”, “cc”]

if “aa” in L1

print(L1)

To determine the number of elements in a list, use the method len().

Ex)    L1= [“a”, “ab”, “aaa”, “cc”]

print(len(L1)) 

List Methods in Python

 

append(): Used to add an element at the end of the list.

 

Ex)    L1= [“a”, “ab”, “aaa”, “cc”]

L1.append(“hg”)

print(L1)

gives you:  [“a”, “ab”, “aaa”, “cc”, [“hg”]]

 

insert(): Used to add an element at any specific position.

 

Ex)    L1= [“a”, “ab”, “aaa”, “cc”]

L1.insert(3, “hg”)

print(L1)

 

remove(): Used to remove an specific element from the list.

 

Ex)    L1= [“a”, “ab”, “aaa”, “cc”]

L1.remove(“ab”)

print(L1)

 

pop(): Used to an element from specific index in a List.

 

Ex)    L1= [“a”, “ab”, “aaa”, “cc”]

L1.pop(2)

print(L1)

 

del(): It is used to delete a List completely.

 

Ex)    L1= [“a”, “ab”, “aaa”, “cc”]

del L1

 

clear(): It is used to clear all elements from the list.

 

Ex)    L1= [“a”, “ab”, “aaa”, “cc”]

L1.clear()

print(L1)

 

copy(): It is used to copy the elements from one List to another.

 

Ex)    L1= [“a”, “ab”, “aaa”, “cc”]

L2=L1.copy()

print(L2)

 

extend(): Extends list by appending elements from the iterable.

 

Ex)    L1= [“a”, “ab”, “aaa”, “cc”]

L1.extend(“qw”)

print(L1)

gives you: [“a”, “ab”, “aaa”, “cc”, “qw”]

 

index(): Returns index of a specific element.

 

Ex)    L1= [“a”, “ab”, “aaa”, “cc”]

print(index(“a”))

 

reverse(): It returns the reverse of a given list.

 

Ex)    L1= [“a”, “ab”, “aaa”, “cc”]

print(reverse(L1))

 

sort(): It returns sorted elements in a List.

 

Ex)    L1= [“a”, “ab”, “aaa”, “cc”]

print(sort(L1))

 

We hope you understand the Python list tutorial with examples and how to create a list in the Python concept. Get success in your career as a Python developer by being a part of the Prwatech, India’s leading Python training institute in Bangalore.

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Quick Support

image image