Loop Control Statements in C Introduction to HTML How to use the Github API The image tag, anchor tag and the button tag Ordered and Unordered Lists in HTML The division tag HTML Forms Tables in HTML Introduction to C Programming Introduction to Python Varibles and Datatypes in Python Operators in Python Typecasting in Python Input and Output in Python If Else in Python Loops in Python Break, Continue and Pass in Python Python practice section 1 Lists in Python Tuple in Python

Lists in Python

Jump to specific sections:

  1. List indexing
  2. List slicing
  3. Lists are mutable
  4. Lists Concatenation
  5. Lists methods
Python lists are used to store set of values. Python lists can store value of any datatype but we generally store values of one datatype in a list.
Example:

# In this way we declare list
# lst1 is list containing only integers
lst1 = [1,2,3,4,5]

# lst2 contains different datatypes
lst2 = ["Rapid", 123, 11.23, False]

print(lst1)
print(type(lst1))
print(lst2)
Output:
[1, 2, 3, 4, 5]
<class 'list'>
['Rapid', 123, 11.23, False]

We can iterate over list using for loop in the following way:

lst = [1,2,3,4,5]

for i in lst:
    print(i)
Output:
    1
    2
    3
    4
    5

Lists can be indexed in following way:


lst = ["Rapid","Coders","Akshay"]
# Every value of list can be accessed by its index
# list  = ["Rapid","Coders","Akshay"]
# index = [0,1,2]

# acessing value present at 0 index
print(lst[0])
print(lst[2])

print()
print("Now lets learn about negative index: ")
print("index of last element is -1")
print("index of last second element is -2 and so on")
print()
print(lst[-1])
Output:
Rapid
Akshay

Now lets learn about negative index: 
index of last element is -1
index of last second element is -2 and so on
    
    Akshay

Lists can be sliced, slicing means getting a part of a sequence.

Syantx:
    list_variable[Start_index : Stop_index]
Slice operations return a new list containing the requested elements. In this new list, we have all elements from start index to stop index, start index is included but stop index is not included.
Example:

lst = ["Rapid","Coders","Akshay"]

print("slicing from index 0 to 1",end=" = ")
print(lst[0:2])
print("slicing from index 1 to 2",end=" = ")
print(lst[1:2])
Output:
slicing from index 0 to 1 = ['Rapid', 'Coders']
slicing from index 1 to 2 = ['Coders']

Lists are mutable, this means we can change their content. Consider following example:

lst = ["Rapid","Coders","Akshay"]
print(lst)

# lets change element of list
lst[2] = "Developers"
print()
print(lst)
Output:
['Rapid', 'Coders', 'Akshay']

['Rapid', 'Coders', 'Developers']

Lists Concatenation: We can merge two lists into a single list using '+' operator.

lst1 = ["Rapid", "Coders"]
lst2 = ["Akshay", "Singh"]

print(lst1)
print(lst2)

print("lst1 + lst 2",end=" = ")
print(lst1 + lst2)
Output:
['Rapid', 'Coders']
['Akshay', 'Singh']
lst1 + lst 2 = ['Rapid', 'Coders', 'Akshay', 'Singh']

Lists methods:

  1. lst.append(value): This method inserts value to the end of list.
  2. lst.insert(index,value): This method inserts value at given index in list.
  3. lst.pop(index): This will delete element which is present at given index.
  4. lst.remove(value): This will remove given value from list.
  5. lst.count(value): This will return the number of times value appears in the list.
  6. lst.clear(): This will remove all elements from list.
  7. lst.sort(): This sorts the list.
  8. lst.reverse(): This reverses the list.
Code:

lst = ['Rapid', 'Coders', 'Akshay']
print(lst)
print()

lst.append("Singh")
print('lst.append("Singh")',end=" = ") 
print(lst)
print()

lst.insert(2,"I am")
print('lst.insert(2,"I am")',end=" = ")
print(lst)
print()
 
lst.pop(2)
print("lst.pop(2)",end=" = ")
print(lst)
print()

lst.remove("Akshay")
print('lst.remove("Akshay"',end=" = ")
print(lst)
print()
 
temp = lst.count("Rapid")
print('lst.count("Rapid")',end=" = ")
print(temp)
print()

lst.sort()
print("lst.sort()",end=" = ")
print(lst)
print()

lst.reverse()
print("lst.reverse()",end=" = ")
print(lst)
print()

lst.clear()
print("lst.clear()",end=" = ")
print(lst)
Output:
['Rapid', 'Coders', 'Akshay']

lst.append("Singh") = ['Rapid', 'Coders', 'Akshay', 'Singh']

lst.insert(2,"I am") = ['Rapid', 'Coders', 'I am', 'Akshay', 'Singh']

lst.pop(2) = ['Rapid', 'Coders', 'Akshay', 'Singh']

lst.remove("Akshay" = ['Rapid', 'Coders', 'Singh']

lst.count("Rapid") = 1

lst.sort() = ['Coders', 'Rapid', 'Singh']

lst.reverse() = ['Singh', 'Rapid', 'Coders']

lst.clear() = []