Learn about Python Lists

Introduction

Python list is a dynamic size array, and here it's not mandatory to declare all the values homogeneous. It can contain different data types, like integer, float, strings, etc. Lists contain comma-separated values between two square brackets.

Example

list_1 = ['Python', 'Java', 'C++', 1999, 2022]

How to access list elements in python


list_1 = ['Python', 'Java', 'C++', 1999, 2020]
# 1st element
print(list_1[1])
# 0th element
print(list_1[0])
# -1 refers to the last element
print(list_1[-1])
# starting and ending ranges are given
print(list_1[1:3])
# 0th to 3rd element
print(list_1[:3])

Output

Java
Python
2020
['Java', 'C++']
['Python', 'Java', 'C++']

How to add elements to list

There are three methods to add elements to a list in python. These are append(), insert(), and extend(). The below example is for you.

Code


list_1 = ['Python', 'Java', 'C++', 1999, 2020]
# Add data to a list using append method
list_1.append("Cotlin")
print(list_1)

# Add an element to a list in a particular position
list_1.insert(1, "Shell")
print(list_1)

list_2 = ["Ruby"]
# Concatenating two lists
list_3 = list_1 + list_2
print(list_3)

# Add one list(list_1) at the end of another list(list_2)
list_1.extend(list_2)
print(list_1)

Output

['Python', 'Java', 'C++', 1999, 2020, 'Cotlin']
['Python', 'Shell', 'Java', 'C++', 1999, 2020, 'Cotlin']
['Python', 'Shell', 'Java', 'C++', 1999, 2020, 'Cotlin', 'Ruby']
['Python', 'Shell', 'Java', 'C++', 1999, 2020, 'Cotlin', 'Ruby']

How to remove an element from a list in python

There are three methods to perform the delete operation on a list. These are pop(), remove(), del(). In the below example, you'll get the usage of each method.


list_1 = ['Python', 'Java', 'C++', 1999, 2020]
# It'll delete the last item
list_1.pop()
print(list_1)

# It'll delete the item from the 1st position
list_1.pop(1)
print(list_1)

# It'll remove a specified item
list_1.remove("C++")
print(list_1)

# Del method removes a specified index
del list_1[0]
print(list_1)

# Clear method empties the entire list.
list_1.clear()
print(list_1)

Output

['Python', 'Java', 'C++', 1999]
['Python', 'C++', 1999]
['Python', 1999]
[1999]
[]

How to count the elements in a list in python

There are two methods to count the elements in a python list. One is len() method; it returns the total number of elements present in a list. Another one is the count() method which tells the number of presence of a specific item in a list. Let's see an example.

Code


list_1 = ['Python', 'Java', 'C++', 1999, 2020]
print(f"The length of list_1 is: {len(list_1)}")


languages = ['Python', 'C++', 'C', 'C#', 'Java', 'Ruby', 'Python']
print("The Number of the presence of 'Python' is: ",languages.count('Python'))

Output

The length of list_1 is: 5
The Number of the presence of 'Python' is:  2

Find index number of a data item from a list

Python offers to find the index number of a data item from a list by using the index() method. The below code will help you to do so.

Code


lang = ['Python', 'C++', 'C', 'C#', 'Java', 'Ruby', 'Python']
# Assigning the data item to a variable
val = 'Java'
# printing the index no. of the given item
print(f"Item: {val}\nIndex no. {lang.index(val)}")

Output

Item: Java
Index no. 4

Subhankar Rakshit

Meet Subhankar Rakshit, a Computer Science postgraduate (M.Sc.) and the creator of PySeek. Subhankar is a programmer, specializes in Python language. With a several years of experience under his belt, he has developed a deep understanding of software development. He enjoys writing blogs on various topics related to Computer Science, Python Programming, and Software Development.

Post a Comment (0)
Previous Post Next Post