List in Python

Shekhar Ravat

9 months ago

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.
List in Python | Insideaiml
List in Python | Insideaiml
 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 −

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 −


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

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

Delete List Elements

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


list_1 = ['physics', 'chemistry', 1997, 2000]
del list_1[2]
print("After deleting value at index position 2 : ")
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']

# again execute same set of code by using extend attribute
list_1 = ['Physics', 'Chemistry']
list_2 = ['Math', 'Science', 'Social']

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