Python Lists

 

A List is a collection of different kinds of values or items. Since Python lists are mutable, we can change their elements after forming.

Below given are some of the programs using Python Lists:

list1 = [1, 2, "Python", "Program", 15.9]
list2 = ["Amy", "Ryan", "Henry", "Emma"]

# printing the list
print(list1)
print(list2)

# printing the type of list
print(type(list1))
print(type(list2))

-----------------------------------------------------------------------------------------------------------------------------

  1. a = [ 1, 2, "Ram", 3.50, "Rahul", 5, 6]
    b = [ 1, 2, "Ram", 3.50, "Rahul", 5, 6]
    a == b

---------------------------------------------------------------------------------------------------------------------------

  1. list = [1,2,3,4,5,6,7]
    print(list[0])
    print(list[1])
    print(list[2])
    print(list[3])
    # Slicing the elements
    print(list[0:6])
    # By default, the index value is 0 so its starts from the 0th element and go for index -1.
    print(list[:])
    print(list[2:5])
    print(list[1:6:2])

-------------------------------------------------------------------------------------------------------------------------

list = [1,2,3,4,5]
print(list[-1])
print(list[-3:])
print(list[:-1])
print(list[-3:-1])

------------------------------------------------------------------------------------------------------------------------

# updating list values
list = [1, 2, 3, 4, 5, 6]
print(list)
# It will assign value to the value to the second index
list[2] = 10
print(list)
# Adding multiple-element
list[1:3] = [89, 78]
print(list)
# It will add value at the end of the list
list[-1] = 25
print(list)

-----------------------------------------------------------------------------------------------------------------------

Python Concatenation:

list1 = [12, 11, 16, 18, 20]
list2 = [9, 10, 22, 54, 86]
# concatenation operator +
l = list1 + list2
print(l)  


------------------------------------------------------------------------------------------------------------------------

Find particular element is present in list

list1 = [100, 200, 300, 400, 500]
# true will be printed if value exists
# and false if not

print(600 in list1)
print(700 in list1)
print(1040 in list1)

print(300 in list1)
print(100 in list1)
print(500 in list1)  

-------------------------------------------------------------------------------------------------------------------------

Adding elements to list

l =[]
#Number of elements will be entered by the user
n = int(input("Enter the number of elements in the list:"))
# for loop to take the input
for i in range(0,n):
# The input is taken from the user and added to the list as the item
l.append(input("Enter the item:"))
print("printing the list items..")
# traversal loop to print the list items
for i in l:
print(i, end = " ")  

-------------------------------------------------------------------------------------------------------------------------

Removing elements from list

list = [0,1,2,3,4]
print("printing original list: ");
for i in list:
print(i,end=" ")
list.remove(2)
print("\nprinting the list after the removal of first element...")
for i in list:
print(i,end=" ")  

------------------------------------------------------------------------------------------------------------------------

Program to eliminate the List's duplicate items.

list1 = [1,2,2,3,55,98,65,65,13,29]
# Declare an empty list that will store unique values
list2 = []
for i in list1:
if i not in list2:
list2.append(i)
print(list2)  

------------------------------------------------------------------------------------------------------------------------


 

Comments

Popular posts from this blog

How to Run Python in MS Visual Studio Code

Python Strings

Python Loop Statements