All Courses

List in Python

Shekhar Ravat

3 years ago

List in Python | Insideaiml
Table of Contents
  • Introduction
  • Accessing Values in Lists
  • Updating Lists
  • Delete List Elements
  • Adding List elements:

Introduction

          A simple variable can not store multiple values. This is where Data Structures of Python concept comes into the picture. In this Data Structure, we have LIST, TUPLE, SET, and DICTIONARIES. In this Article we majorly discuss Lists.
 A list is a collection of different types of data types, where we can store int, float, string, and boolean. The list is mutable or changeable The values or Items should be enclosed with squared sections. For instance -
list_1 = [96, 97.5, 'Physics','Chemistry']
list_2 = [1, 2, 3, 4, 5 ]
list_3 = ["a", "b", "c", "d"]
Indexing starts from 0. If we want to extract elements we should use indexing concept.

Accessing Values in Lists

          To get to values in records, utilize the square sections for cutting alongside the list of files to get esteem accessible at that list. For instance −
#!/usr/bin/python

list_1 = [96, 97.5, 'Physics','Chemistry']
list_2 = [1, 2, 3, 4, 5 ]
print("list_1[2]: ", list_1[2])
print("list_2[2:]: ", list_2[2:])
For the above code, we will get the following output
list_1[2]:  Physics

list_2[2:]:  [3, 4, 5]

Updating Lists

          We can update the values or elements or items present in the list variable by using the concept of the index. For instance −

#!/usr/bin/python

list = ['physics', 'chemistry', 1997, 2000]
print("Value available at index 2 : ")
print(list[2])
list[2] = 2001
print("New value available at index 2 : ")
print(list[2])
For the above code, we will get the following output

Value available at index 2 :
1997
New value available at index 2 :
2001

Delete List Elements

          If we want to delete any element we can use two attributes, that are del and remove. For instance −

#!/usr/bin/python

list_1 = ['physics', 'chemistry', 1997, 2000]
print(list_1)
del list_1[2]
print("After deleting value at index position 2 : ")
print(list_1)
print("=============")
list_1.remove('physics)
print(list_1)
For the above code, we get the following output -

['physics', 'chemistry', 1997, 2000]
After deleting value at index position 2 :
['physics', 'chemistry', 2000]
=============
['chemistry, 2000]
The Basic difference between remove and del is, remove helps to takeoff the elements by using values present in the variable and del helps to takeoff elements by using index position of the value.

Adding List elements

          If we want to insert new elements into the existing list variable we use append and extend attributes. The basic difference between append and extend is-
append() attribute forms Nested list and extend() attribute helps to combine all the elements that are contained in the lists. For instance- 
list_1 = ['Physics', 'Chemistry']
list_2 = ['Math', 'Science', 'Social']

list_1.append(list_2)
print(list_1)
print("===========================")
# again execute same set of code by using extend attribute
list_1 = ['Physics', 'Chemistry']
list_2 = ['Math', 'Science', 'Social']

list_1.extend(list_2)
print(list_1)
For the above code we will get the following output-
['Physics', 'Chemistry', ['Math', 'Science', 'Social']]
===========================
['Physics', 'Chemistry','Math', 'Science', 'Social']
I hope you enjoyed reading this article and finally, you came to know about List in Python.
For more such blogs/courses on data science, machine learning, artificial intelligence and emerging new technologies do visit us at InsideAIML.
Thanks for reading…
Happy Learning…

Submit Review