In this tutorial, we will learn about the List in Python. Its types, and how to use them with the help of examples.

List is use to store the group of values. A list can be compose by storing a sequence of different type of values separated by commas. List is mutable object so we can do the manipulations. A list is enclose between square [] brackets. In list insertion order is preserve it means in which order we insert element same order output is print. A List duplicate objects are allowed. The list contains forward indexing & backward indexing.

List in Python
List in Python

The Creates a Lists Python

# Creating a List  
List = []  
print(List)  
     
# Creating a list of strings
List = ['Basic Engineer', 'Student'] 
print(List)  
     
# Creating a Multi-Dimensional List  
List = [['Basic', 'Engineer'], ['Hello Student']]  
print(List)
Example of lists in python

Let’s run the code

Example of list in python
Example of lists in python

Accessing Elements From The List

# single-Dimensional List

List1 = ['Basic', 'Engineer', 'Student']
print("single-demensional list")
print(List1[0])
print(List1[2])

# Multi-Dimensional List

List2 = [['Basic', 'Engineer', 'Student'], ['Hello','Student']]
print("Multi-Dimensional list")
print(List2[0][2])
print(List2[1][1])

# negative list
List3 = [1, 2, 'Basic', 4, 'Engineer', 6, 'Student']
print("negative")
print(List3[-2])
print(List3[-7])
Accessing elements from the List

Let’s run the code

Accessing elements from the List in Python
Accessing elements from the List

Python Lists Operations

  • Repetition
    • Symbol of Repetition is a *
  • Concatenation
    • Symbol of Concatenation +
  • Length
  • Iteration
  • Membership

The Example of Python Lists Operations

# repetition    
list1 = [2, 4, 6, 8, 10, 12, 14]    
a = list1 * 2  
print(a)

# concatenation   
list2 = [2, 4, 6, 8, 10, 12, 14] 
list3 = [3, 6, 9, 12, 15, 18, 21]   
a = list2 + list3  
print(a)

# lenght    
list4 = [2, 4, 6, 8, 10, 12, 14]    
len(list4) 

# iterating 
list5 = [2, 4, 6]  
for i in list5:   
    print(i) 

# membership   
list6 = [2, 4, 6, 8, 10, 12, 14]
print(6 in list6)  
print(7 in list6)  
print(10 in list6)      
Example of Python Lists Operations

Let’s run the code

Example of Python Lists Operations
Example of Python Lists Operations

Method of Lists in Python

MethodDescription
append()add an item to the end of the list
extend()add items of lists and other iterables to the end of the list
insert()inserts an item at the specified index
remove()removes item present at the given index
pop()returns and removes item present at the given index
clear()removes all items from the list
index()returns the index of the first matched item
count()returns the count of the specified item in the list
sort()sort the list in ascending/descending order
reverse()reverses the item of the list
copy()returns the shallow copy of the list
A Method of list in Python

Slicing of Python Lists

It is possible to access a section of items from the list using the slicing operator :

Syntax

list_varible(start:stop:step) 
Where,
The start denotes the starting index position of the list.
The stop denotes the last index position of the list.
The step is use to skip the nth element within a start:stop   
Syntax of Slicing of Python Lists

A Example of Slicing of Python Lists

# slicing

list7 = ['b','a','s','i','c','e','n','g','i','n','e','e','r', 1,2,3]
print(list7[8:15])
print(list7[9:])
print(list7[:])
Example of Slicing of Python Lists

Let’s run the code

Example of Slicing of Python Lists
Example of Slicing of Python Lists

If you have any queries regarding this article or if I have missed something on this topic, please feel free to add in the comment down below for the audience. See you guys in another article.

To know more about Python Wikipedia please click here .

Stay Connected Stay Safe, Thank you

Categories: Software

Basic Engineer

Hey Readers! We have more than fifteen years of experience in Software Development, IoT, Telecom, Banking, Finance and Embedded domain. Currently we are actively working on Data Science, ML and AI with multiple market leaders worldwide. Happy Reading. Cheers!

0 Comments

Leave a Reply

Avatar placeholder

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