Python Lists
Python list used to store the collection of data in a single variable, then there are four ways to store the data in python programming one of them is Python Lists. In python programming, we can create a list using the square brackets []. We can place any number of value in the list
There are four collection data types in the Python programming language
- List
- Tuple
- Set
- Dictionary
Python Lists
are the collection of data that is ordered and changeable and allows duplicate members.
A tuple
is a collection that is ordered and unchangeable and Allows duplicate members.
Python Sets is a collection that is unordered and unindexed and doesn’t allow duplicate members.Dictionary
is a collection which is unordered, changeable and indexed and doesn’t allow duplicate members.
In this tutorial, we are going to learn about printing the elements of a list, modifying elements, deleting elements of the list, and perform many other functions like sum()
, len()
, max()
, min()
and many more.’
Python Lists
Python Lists are the collection of the data. Each element of the list has a specific order. We can access each list element by the index number. The first element of the list has a base index i.e. 0.
Syntax of Python List
In Python programming, list elements are written within the square brackets such as:
list_name=[]
Printing a List
Printing a list is simple like printing variables. With the help of one print statement, we can print the list.
Example of printing a List
my_list = ["PHP", "Python", "Java"]
print(my_list)
[‘PHP’, ‘Python’, ‘Java’]
Topics to remember about Python List
Now, we are going to perform different operations on the list one by one. Don’t forget to see the example provided
Printing a specific element from the list
As of now, we have studied that the list elements are indexed. S. it’s easy to print the specific element of the list by specifying it’s index number.
Example of printing a specific element from the list
my_list = ["PHP", "Python", "Java"]
print(my_list[1])
Python
Printing elements in between some range from the list
Just like printing specific elements, we can also print the elements within some range as well.
Example of printing list in between some range
my_list = ['T', 'u', 't', 'o', 'r', 'i', 'a', 'l', 's', 'C', 'l', 'a', 's', 's']
# elements 4th to 8th
print(my_list[3:8])
[‘o’, ‘r’, ‘i’, ‘a’, ‘l’]
Modifying elements of a list
Above we have studied that the list is changeable. So, it’s easy to modify the list elements.
Example of updating a element in list
my_list = ["PHP", "Python", "java"]
my_list[0] = "JavaScript"
print(my_list)
[‘JavaScript’, ‘Python’, ‘java’]
Checking length of the list
With the help of len()
function, we can check the length of the string.
Example to calculate the length of a list
my_list = ['T', 'u', 't', 'o', 'r', 'i', 'a', 'l', 's', 'C', 'l', 'a', 's', 's']
print(len(my_list))
14
Python Lists – Adding elements
We can easily add an element to the list by using append()
function.
Example to adding a element at end of list
my_list = ["apple", "banana", "cherry"]
my_list.append("orange")
print(my_list)
[‘apple’, ‘banana’, ‘cherry’, ‘orange’]
Adding an element to the specific position
By using insert()
function, we can add an element at any specific position by specifying it’s index to the list.
Example to add a element at specific position in list
thislist = ["apple", "banana", "cherry"]
thislist.insert(1, "orange")
print(thislist)
[‘apple’, ‘orange’, ‘banana’, ‘cherry’]
Adding multiple elements to the list
With the help of the extend()
function, we can add multiple elements at the end of the list.
Example to add multiple elements to list
my_list = ["apple", "banana", "cherry"]
my_list.extend(["1kg", "2 dozen", "500g"])
print(my_list)
[‘apple’, ‘banana’, ‘cherry’, ‘1kg’, ‘2 dozen’, ‘500g’]
Python Lists – Removing specific elements
We can remove any number of elements from the list by specifying it’s value.
Example to remove a specific element by specifying it’s value.
my_list = ["apple", "banana", "cherry"]
my_list.remove("banana")
print(my_list)
[‘apple’, ‘cherry’]
By specifying it’s index number
We can also remove element from the list by specifying it’s index number.
Example to remove element by specifying its index number
my_list = ["apple", "banana", "cherry"]
del my_list[0]
print(my_list)
[‘banana’, ‘cherry’]
Python Lists – Making the whole list empty
We can also remove all the elements from the list by using clear()
function.
Example to make whole list empty
my_list = ["apple", "banana", "cherry"]
my_list.clear()
print(my_list)
[]
Python Lists – Deleting the list
With the help of del keyword, we can easily delete a list.
Example to delete a list completely
my_list = ["apple", "banana", "cherry"]
del my_list
Note: The output is empty because we have deleted the list.
Checking if any item exists in Python Lists
We can easily check if a element is present in the list or not.
Example to check element is exist in list or not
my_list = ["Ramesh", "Suresh", "Rakesh"]
if "Ramesh" in my_list:
print("Yes, 'Ramesh' is in this list")
else:
print("No, 'Ramesh' is not in this list")
Yes, ‘Ramesh’ is in this list
Counting how many times does a specific value exists in Python List
With the help of count()
method, we can also count the frequency of a element in a list.
Example to count the number of times does a value exists in a list.
my_list= [1,2,2,1,2,2,1,"Rakesh"]
print(my_list.count(2))
4
Here you can see, that specific value present 4 times in our list.
Finding the maximum value in the List
We can also find the maximum value in a list by using max()
function.
Example to find maximum value in list
my_list= [1,2,2,1,2,3]
print(max(my_list))
3
Finding the minimum value in the List
With the help if the min()
we can also find the minimum value in a list.
Example to find out minimum value in list
my_list= [1,2,2,1,2,3]
print(min(my_list))
1
Finding the sum of values in the Tuple
We can easily the sum of all the values of a list by using sum()
method.
Example to calculate sum of all the values of a list
my_list= [1,2,2,1,2,3]
print(sum(my_tuple))
11
Finding the average of values in the list
Like the list, we can calculate the average easily.
Example to calculate the average of all list elements
my_list= [1,2,2,1,2,3]
print(sum(my_list)/len(my_list))
1.8333333333333333
Finding the position of any value in Python List
In the List, we can also check at what position does a specific element exist by using index(
) function.
Example to find out the position of a element in list
my_list= ["Ramesh","Suresh","Kamlesh"]
print(my_list.index("Suresh")) # return 1, so the position will be second
1
Exercises & Assignments |
---|
No Content Found. |
Interview Questions & Answers |
---|
No Content Found. |