All Courses

Lists in Python

Pallavi Dhotre

2 years ago

Lists in Python | insideAIML
Table of Content
Introduction
Python lists
How to create list?
How to Get Values from List?
How to use Python list methods to perform python list operations?
  • Insert element/ Create list using append():
  • Remove element from list python remove() function and del keyword
  • Python List count()
  • extend()
  • insert()
  • pop()
  • sort()
  • Python lists concatenation and replication
  • len()
Convert list to string python program
Summary

Introduction 

          In the previous articles, we have discussed variables and we also used variables in various programs. However, variables can hold only one value at a time. In real life, there are many situations where we need to store multiple values. For example, a list of employee names can't be inserted in a single string. If we try, we will have to declare multiple string variables, one for each employee. This may become unmanageable. To avoid this situation python lists are provided. In this article, we are going to have a brief look at python lists, how to create lists, various python list operations, and python list methods. 

Python lists

          Python lists are types of data structures that can hold multiple values under a single common name. We can define a list in python as a collection of elements where each element is assigned a unique number known as an index. Entries in the python lists are known as elements or items.  Python lists are ordered and mutable also list elements are stored at different index positions so python lists allow duplicate entries.  We can think of a list as marks of students. And we can define this as:
Marks=[45,56,67,80]

How to create a list? 

          To create python lists we use square brackets and elements are comma-separated. We do not use any method to create python lists instead we use the following ways
demo_list=[] #empty list
demo_list=[1,2,3,4] #list with integer values
demo_list=['Ram','Sham','Siya'] #list of strings

How to Get Values from List?

          To get an entry from the list we use index numbers of the list. See the following example for more details:
demo_list=['Ram','Sham','Siya'] #list of strings
print(demo_list[0]) #this will access the first element
print(demo_list[1]) #this will access the second element
print(demo_list[2]) #this will access the third element
Output:
Ram
Sham
Siya

How to use Python list methods to perform python list operations?

          There are several python list operations like add elements to the list, remove element from list python, calculating the length, and many more. Following are those methods with the operations

Insert element/ Create list using append():

          This is another way of creating python lists. We create an empty list and then add elements to the list by using the append() function. This approach is used when we don’t know the elements of python lists before running the program. This approach is a dynamic approach to list creation. We can use the append() function on an existing list too – you don't need to check if the list is empty python can insert the elements in the existing list. 
Suppose, you want to store marks of 5 subjects. You declare an empty list, run a loop 5 times, take input in each iteration and add (also called python append list) to the list.
See the following code to create a list using the append() method
marks = []  # define an empty list
for i in range(5):
    a = int(input("Enter the marks:")) #accepting marks for 5 times
    marks.append(a) #python append list,using append() function to push the element into the empty list
print(marks) #display list
Output
Enter the marks:45
Enter the marks:55
Enter the marks:66
Enter the marks:77
Enter the marks:88
[45, 55, 66, 77, 88]

Remove element from list python remove() function and del keyword

          If we don’t know the index number of the element in the list, we can use the remove() method to delete it by value. If there is data duplication in the list, then remove() will remove the first item found.
See the following code:
demo=['sub1','sub2','sub3'] #listprint(demo) #display list before removing the elementdemo.remove('sub1') #remove element from list python remove()print(demo) #list after removing element
Output
['sub1', 'sub2', 'sub3']
['sub2', 'sub3']
          The second way to remove element from list is a del keyword. If you the element index then you can directly remove the data from the list
See the following code:
demo=['sub1','sub2','sub3'] #list
print(demo) #display list before removing the element
del demo[1] #remove second element
print(demo) #list after removing element
Output
['sub1', 'sub2', 'sub3']
['sub1', 'sub3']

Python List count()

          Python list count() method returns the number of occurrences of the specific element. See the following code
demo=['sub1','sub2','sub3','sub1','sub2'] #list
print(demo) #display list before removing the element
print(demo.count('sub1')) #this will display the number of occurrences of the specific items
output
['sub1', 'sub2', 'sub3', 'sub1', 'sub2']
2

extend()

          This method can be used for combining two lists. See the code given below
a=['Ana','John','Nick'] #first list
b=['Harry','Peter'] #second list
a.extend(b) #combining two python lists using extend()
print(a) #display final list
Output
['Ana', 'John', 'Nick', 'Harry', 'Peter']

insert()

          This method can insert the element at a specific location. Example Code is given below
a=['Ana','John','Nick'] #first list
a.insert(3,'Neha') #inserting element at index 3 in list 
print(a) #display list
 Output
['Ana', 'John', 'Nick', 'Neha']

pop()

          This method returns the object which is removed from the list. Usually, it removes the last item of the list. Example code is given below
a=['Ana','John','Nick'] #first list
print(a.pop()) #removes the last element
print(a) #display list
 Output
Nick
['Ana', 'John']

sort()

          This method sorts the elements in the list. Let’s see the code given below
a=[10,20,55,44,34,9,6] #first list
a.sort() #sorts the given list
print(a) #display list
Output
[6, 9, 10, 20, 34, 44, 55]

Python lists concatenation and replication 

          Python lists are concatenated using + operator and for python lists replication we use * operator. Example code is given below
a=[10,20,55,44,34,9,6] #first list
b=['hi'] # second list
print(a+b) #python lists concatenation
print(b*4) #python lists replication
output
[10, 20, 55, 44, 34, 9, 6, 'hi']
['hi', 'hi', 'hi', 'hi']

len()

          this function returns the length of the lists. 
a=[10,20,55,44,34,9,6] #first list
print(len(a))#display the length of the list
Output
7
So, these are some functions we use in the python list. We can also convert the list into string. 

Convert list to string python program

#convert list to string python program
demo=['inside','AI','ML']
str='' #empty string
for ele in demo: #iterating through the list
    str += ele #converting the list elements to string
print(str) #display string
Output
insideAIML

Summary

          In this article, we discussed python lists, how to create list, various python list operations, and python list methods. Also, we learned to create list, remove elements from list python remove(). We can define list in python as a collection of elements where each element is assigned a unique number known as an index. Entries in the python lists are known as elements or items.  Python lists are ordered and mutable. Also, we have seen how to convert list to string python program. We hope you enjoyed the article. If you have any related queries, feel free to ask in the comment section below.
     
Liked what you read? Then don’t break the spree. Visit our blog page to read more awesome articles. 
Or if you are into videos, then we have an amazing Youtube channel as well. Visit our InsideAIML Youtube  Channel to learn all about Artificial Intelligence, Deep Learning, Data Science and Machine Learning. 
Keep Learning. Keep Growing.
       

Submit Review