List in python is used to store the sequence of various type of data. In python a list can be written as a list of comma separated values between square brackets.

Note:

A list is ordered, mutable, and it allows duplicate elements.

Eg:

mylist = ["Red","Green","Yellow"]
print(mylist)

OUTPUT:

["Red","Green","Yellow"]

In python we can also create an empty list.

Eg:

mylist = list()
print(mylist)

OUTPUT:

[]


Access Elements in List


We can access an item in a list by referring to the index number.

Example:

my_list = ["red", "green", "yellow"]
print(my_list[1])

Output:

green

Negative Indexing




In Python we can access the item's in the list  by referring Negative indexing for its sequences.

For example if we take a list with 4 items, the index of -1 refers to the last item and the index of -4 refers to the last item.

Example:

my_list = ["green", "red", "blue", "yellow", "pink", "white"]
print(my_list[-4])

Output:

blue

Range of Index in a  List




In Python we can access a range of items in a list by using [: colon], we can specify from where to start and where to end the range.

Example:

my_list = ["green", "red", "blue", "yellow", "pink", "white"]
print(my_list[2:4])

Output:

['blue', 'yellow']

Add new item to a list


In Python we can add item in a list by using Python built-in function called append(). We can add only one item at a time using append() function if we want to add multiple items we want to use loops (or) extend() function.

Example for adding one item:

my_list = ["green", "red", "blue", "yellow", "pink", "white"]
my_list.append("brown")
print(my_list)

Output:

['green', 'red', 'blue', 'yellow', 'pink', 'white', 'brown']

Example for adding multiple items using Loops:

List=['1', '2']
for i in range(3, 6): 
    List.append(i) 
print("\nList after Addition of elements from 3-5: ") 
print(List) 

Output:

List after Addition of elements from 3-5:
['1', '2', 3, 4, 5]

Example for adding multiple items using extend():

my_list = ['1', '2', '3']
my_list.extend(['4', '5', '6'])
print(my_list)

Output:

['1', '2', '3', '4', '5', '6']

Update item value


In python we can update the value of a specific item by referring to its index number.

Example:

my_list = ["Red", "Green", "Blue"]
my_list[1] = "Yellow"
print(my_list)

Output:

['Red', 'Yellow', 'Blue']

Delete a item from a list


We can delete a particular element in python list by referring its index number.

Example:

my_list = ["Red", "Green", "Blue", "White", "Yellow"]
del my_list[2]
print(my_list)

Output:

['Red', 'Green', 'White', 'Yellow']

Delete multiple items in a list


To delete multiple items in a list we want to specify the range of index of the list.

Example:

my_list = ["Red", "Green", "Blue", "White", "Yellow", "Sky-blue", "Black"]
del my_list[2:5]
print(my_list)

Output:

['Red', 'Green', 'Sky-blue', 'Black']

Delete entire list


To delete the entire list we want to give specify the list name.

Example:

my_list = ["Red", "Green", "Blue", "White", "Yellow", "Sky-blue", "Black"]
del my_list
print(my_list)

Output:

Traceback (most recent call last):
  File "D:\program\python\Advanced Python\problem.py", line 3, in <module>
    print(my_list)
NameError: name 'my_list' is not defined

remove() method


Using remove() method we can  remove the given item in a list.

Example:

my_list = ['E', 'X', 'A', 'M', 'P', 'L', 'E']
my_list.remove('M')
print(my_list)

Output:

['E', 'X', 'A', 'P', 'L', 'E']

pop() method


Using pop() method we can pop particular element in a list by referring its index number. If the index number is not specified it will pop the last element in the list. This pop() method is used in data structure for implementing lists as stack.

Example:

my_list = ['E', 'X', 'A', 'M', 'P', 'L', 'E']
my_list.pop()
print(my_list)

Output:

['E', 'X', 'A', 'M', 'P', 'L']

clear() method


If we use del() method it will delete the entire list. If we want the clear the items in a list we can use clear() method.

Example:

my_list = ['E', 'X', 'A', 'M', 'P', 'L', 'E']
my_list.clear()
print(my_list)

Output:

[]

len() method


We can the length of a list by using len() method.

Example:

my_list = ['E', 'X', 'A', 'M', 'P', 'L', 'E']
print(len(my_list))

Output:

7

Copy() method


We can copy a list to another list by using copy() method. When we change a value in our list it will be automatically updated in the new list.

Example:

my_list1 = ['E', 'X', 'A', 'M', 'P', 'L', 'E', 'S']
my_list2 = my_list1.copy()
print(my_list2)

Output:

['E', 'X', 'A', 'M', 'P', 'L', 'E', 'S']

Post a Comment

Previous Post Next Post