Data Science and Machine Learning Internship ...
- 22k Enrolled Learners
- Weekend/Weekday
- Live Class
Python programming language has emerged as the hottest programming language nowadays. Developers have realized the importance of efficient implementation rather than writing complex programs. Python serves the developers with, out of the box features and applications, one such concept is lists in python. It is a collection data type which is often preferred to store ordered data in python. Following are the concepts discussed in this blog:
List is a collection data type in python. It is ordered and allows duplicate entries as well. Lists in python need not be homogeneous, which means it can contain different data types like integers, strings and other collection data types. It is mutable in nature and allows indexing to access the members in a list.
To declare a list, we use the square brackets.
List is like any other array that we declare in other programming languages. Lists in python are often used to implement stacks and queues. The lists are mutable in nature. Therefore, the values can be changed even after a list is declared.
mylist = [0,1,2,3,4,5,6]
Indexing :
To access a value from a list, we use the index values. Below is the code to get the letter ‘A’ from the list containing the letters of the word ‘EDUREKA’.
a = ['E', 'D', 'U', 'R', 'E', 'K', 'A'] print(a[6]) print(a[-1])
Both the print statements will fetch letter ‘A’ from the list.
While choosing a data type for storing our data, we must keep in mind the properties and features of the data type. It becomes more efficient and secure if we make the right choice in first place.
A list is preferred because it can store multiple data at the same time. It becomes easy to replace and modify the values inside a list. We can store the sequence in a list and perform several iterations using the loops as well. There are numerous operations we can perform on a list as well, lets understand the various operations that we have for lists in python.
Following are the operations that we can perform on a list.
append
a = [1,2,3,4,5] a.append(6) print(a) #the output will have 6 at the end of the list.
clear
a = [1,2,3,4,5] a.clear() #this will clear the list or empty the list.
copy
a = [1,2,3,4,5] b = a.copy() print(b) #it makes the copy of the list.
count
a = [1,1,1,3,3,3,4,4,4,4,5,5,5,5,5] a.count(5) #this will give the number of times 5 is present in the list.
extend
a = [1,2,3,4,5] a.extend(range(6,11)) #this will add the values in this list from the iterable object range.
insert
a = ['edureka', 'python', 'data science'] a.insert(2,'artificial intelligence') #this will add the string at the index value 2
index
a = ['edureka', 'python', 'programming', 'data science', 'AI', 'machine learning'] a.index('data science') #this will get the index value at the string 'data science' which is 3.
pop
a = [1,2,3,4,5] a.pop() #this will pop the value from the end of the list i.e 5. the list will no longer have 5 after this.
remove
a = [1,2,3,4,11,5] a.remove(11) #this will remove 11 from the list.
reverse
a = [5,4,3,2,1] a.reverse() #this will reverse the list. #another statement to reverse the list a = a[: :-1]
sort
a = [3,1,2,6,4,5,9,6,7,8] a.sort() #you will get a sorted list as a result.
Replacing a value in a list
a = ['edureka', 'python', 'data science', 'tennis', 'machine learning'] a[3] = 'artificial intelligence' #this will replace the value at the given index with the mentioned value.
Iterate through a list
Lists can be used for iterations as well. Below is the code to iterate a list and print values using a control statement.
a = [1,2,3,4,5] for x in a: if x == 4: break print(x) #this will iterate through the list and print the values until it encounters 4.
The List Constructor
The list constructor is used to create/declare a list.
a = list((1,2,3,4,5)) print(a) #you will get a list with the values declared in the constructor.
As you can see, the list constructor takes the tuple as argument. Similarly, you can declare any other data type like a dictionary or a set inside the list constructor as well.
Suppose you have a list with numbers from 0-10. But you only want to get the numbers from 5-10, you must not access all the elements typing the index values of all those numbers. Instead you can follow the approach in the code below.
a = [1,2,3,4,5,6,7,8,9,10] a[4:11] #this will get all the numbers starting from index 4 to index 11. a[-1:-6] #this will get all the numbers from the index 11 to index 6. a[4:] #this will print all the numbers starting from index 4 until the end of the list. a[:6] #this will print all the numbers from index 0 until the index 6.
Subsetting a list means, declaring a list inside an existing list.
a = list(range(5,11) b = [1,2,3,4, a ] #to access a value in the list b[4] #this will print the list a. b[4][4] #this will get the value at the index value 4 in the list a. b[4][4] = 19 #we can change the values as well, replace, delete modify etc.
Instead of a list, we can use any other data type as well. But since a set is unindexed, it will not be possible to access the set items separately using the index values.
In this blog, we have discussed the lists in python and all the operations that we can perform. Lists in python is a very important concept which plays an important role while learning the basics of python programming. Python programming language has many out of the box features, with the applications and implementations it has become one of the most popular programming language nowadays. You can also enroll in python programming certification to kickstart your learning.
Have any questions? mention them in the comments, we will get back to you.
Course Name | Date | Details |
---|---|---|
Python Programming Certification Course | Class Starts on 28th December,2024 28th December SAT&SUN (Weekend Batch) | View Details |
Python Programming Certification Course | Class Starts on 25th January,2025 25th January SAT&SUN (Weekend Batch) | View Details |
edureka.co