List in Python


Lists are used to store multiple items in a single variable.To separate two items, you use a comma ( , ) . List uses the square brackets [ ]. Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage.

  • Sequence Type
  • Element of the list can access by index
  • They are mutable

Source Code

This program creates a list called "a" which contains the elements 1, 2, 3, 4, and 5. The first print(a) statement prints the entire list. The second print(type(a)) statement prints the type of the variable, which in this case is a list. The next line, a[0] = 100, modifies the first element of the list to be 100. The next print(a) statement prints the modified list with the first element as 100.

The next lines, print("Slicing"), print(a[1]), print(a[-1]), print(a[0:3]), print(a[2:]), and print(a[:3]) are all examples of "slicing", which allows you to extract specific elements from a list by specifying the starting and ending indices.

  • print(a[1]) prints the second element of the list, which is 2.
  • print(a[-1]) prints the last element of the list, which is 5.
  • print(a[0:3]) prints a sublist of the first three elements, which is [100, 2, 3]
  • print(a[2:]) prints sublist starting from index 2 till the end, which is [3, 4, 5]
  • print(a[:3]) prints sublist starting from index 0 till 2, which is [100, 2, 3]
# List in Python
"""
Sequence Type
Mutable
a[5]
a={1,2,3,4,5}
a[0]
"""
a = [1, 2, 3, 4, 5]
print(a)
print(type(a))
a[0] = 100
print(a)
print("Slicing")
print(a[1])
print(a[-1])
print(a[0:3])
print(a[2:])
print(a[:3])
print("-----------------------------")

This program creates a list called "a" which contains a variety of elements including integers, booleans, strings, and even a nested list. The first print(a) statement prints the entire list. The second print(type(a)) statement prints the type of the variable, which in this case is a list.

The next lines, print(a[0], " type is ", type(a[0])), print(a[1], " type is ", type(a[1])), print(a[2], " type is ", type(a[2])), print(a[3], " type is ", type(a[3])), print(a[4], " type is ", type(a[4])) are used to print the element and it's type from the list.

  • print(a[0], " type is ", type(a[0])) prints the first element of the list which is 1, and it's type which is int
  • print(a[1], " type is ", type(a[1])) prints the second element of the list which is True, and it's type which is bool
  • print(a[2], " type is ", type(a[2])) prints the third element of the list which is 'Ram', and it's type which is str
  • print(a[3], " type is ", type(a[3])) prints the fourth element of the list which is 2.5, and it's type which is float
  • print(a[4], " type is ", type(a[4])) prints the fifth element of the list which is [1, 2, 3, 4], and it's type which is list

The last line print(a[4][1]) prints the second element of the nested list which is 2. Here, the first set of brackets a[4] refer to the nested list and the second set of brackets [1] refer to the index of the element within that nested list.

a = [1, True, 'Ram', 2.5, [1, 2, 3, 4]]
print(a)
print(type(a))
print(a[0], " type is ", type(a[0]))
print(a[1], " type is ", type(a[1]))
print(a[2], " type is ", type(a[2]))
print(a[3], " type is ", type(a[3]))
print(a[4], " type is ", type(a[4]))
print(a[4][1])
print("-----------------------------")

This program creates a list called "a" which contains four elements: 10, 25, 35, and 45. The first print(a) statement prints the entire list.

  • a.clear(), is used to clear all elements of the list. The next print(a) statement prints an empty list because all the elements have been removed.
  • b = a.copy(), creates a copy of list 'a' and assigns it to a new variable 'b'. The next print(b) statement prints the copied list which is the same as the original list 'a'.
  • a = [10, 25, 35, 45, 25, 4, 25] updates the list 'a' with new elements. The next line, print(a.count(25)) prints the count of element 25 in the list, which is 3
  • print(a.index(25)) prints the index of the first occurrence of the element 25 in the list, which is 1
  • print(len(a)) prints the length of the list, which is 7
  • print(max(a)) prints the maximum element of the list, which is 45
  • print(min(a)) prints the minimum element of the list, which is 4
  • print(a) prints the list
  • a.pop(0) removes the element at index 0 and returns the removed element which is 10 in this case. The last print(a) statement prints the updated list with the first element removed.
  • a.remove(25) removes the first occurrence of the element 25 in the list. The last print(a) statement prints the updated list with the element 25 removed.
a = [10, 25, 35, 45]
print(a)
a.clear()
print(a)
a = [10, 25, 35, 45]
b = a.copy()
print(b)
a = [10, 25, 35, 45, 25, 4, 25]
print(a.count(25))
print(a.index(25))
print(len(a))
print(max(a))
print(min(a))
print(a)
a.pop(0)  # remove Element using index
print(a)
a = [10, 25, 35, 45, 25, 4, 25]
a.remove(25)  # Values
print(a)
print("-----------------------------")

This program creates a list called "names" which initially contains one element "Ram". The first print(names) statement prints the entire list. The next few lines, names.append("Sam"), names.append("Ravi"), names.append("Kumar") are used to add elements "Sam", "Ravi" and "Kumar" to the list "names" using the append() method. The second print(names) statement prints the updated list with the added elements.

The next line, name2 = ["Sara", "Anitha"] creates a new list called name2. The next line, names.extend(name2) is used to add the elements of name2 to the list names using the extend() method. The next print(names) statement prints the updated list with the added elements of name2. The next line, names.insert(0,"Suriya") is used to insert an element "Suriya" at index 0 of the list "names" using the insert() method. The last print(names) statement prints the updated list with the newly inserted element

names = ["Ram"]
print(names)
names.append("Sam")
names.append("Ravi")
names.append("Kumar")
print(names)
name2 = ["Sara", "Anitha"]
names.extend(name2)
print(names)
names.insert(0,"Suriya")
print(names)
print("-----------------------------")

This program demonstrates various built-in list functions and methods.

  • print(list(range(5))), creates a list containing the numbers from 0 to 4 using the range() function and prints it.
  • print(list("Tutorjoes")), creates a list containing each character of the string "Tutorjoes" as a separate element and prints it.
  • a=[10,50,100,25,85], creates a list called 'a' with 5 elements. The next line, print(a), prints the list
  • a.sort(), sorts the list in ascending order using the sort() method. The next line, print(a), prints the sorted list
  • a.sort(reverse=True), sorts the list in descending order using the sort() method with the reverse=True argument. The next line, print(a), prints the sorted list in descending order
  • a=["Orange","Apple","Zebra"] updates the list 'a' with new elements. The next line, a.sort() sorts the list in alphabetical order using the sort() method. The next line, print(a), prints the sorted list
  • a.sort(reverse=True) sorts the list in reverse alphabetical order using the sort() method with the reverse=True argument. The next line, print(a), prints the sorted list in reverse alphabetical order
  • a=["Orange","Apple","Zebra"] updates the list 'a' with new elements. The next line, a.sort(key=len) sorts the list based on the length of the elements using the sort() method with the key=len argument. The next line, print(a), prints the sorted list based on the length of the elements.
print(list(range(5)))
print(list("Tutorjoes"))
a=[10,50,100,25,85]
print(a)
a.sort()
print(a)
a.sort(reverse=True)
print(a)
a=["Orange","Apple","Zebra"]
a.sort()
print(a)
a.sort(reverse=True)
print(a)
a=["Orange","Apple","Zebra"]
a.sort(key=len)
print(a)

To download raw file Click Here

Output

[1, 2, 3, 4, 5]

[100, 2, 3, 4, 5]
Slicing
2
5
[100, 2, 3]
[3, 4, 5]
[100, 2, 3]
-----------------------------
[1, True, 'Ram', 2.5, [1, 2, 3, 4]]

1  type is  
True  type is  
Ram  type is  
2.5  type is  
[1, 2, 3, 4]  type is  
2
-----------------------------
[10, 25, 35, 45]
[]
[10, 25, 35, 45]
3
1
7
45
4
[10, 25, 35, 45, 25, 4, 25]
[25, 35, 45, 25, 4, 25]
[10, 35, 45, 25, 4, 25]
-----------------------------
['Ram']
['Ram', 'Sam', 'Ravi', 'Kumar']
['Ram', 'Sam', 'Ravi', 'Kumar', 'Sara', 'Anitha']
['Suriya', 'Ram', 'Sam', 'Ravi', 'Kumar', 'Sara', 'Anitha']
-----------------------------
[0, 1, 2, 3, 4]
['T', 'u', 't', 'o', 'r', 'j', 'o', 'e', 's']
[10, 50, 100, 25, 85]
[10, 25, 50, 85, 100]
[100, 85, 50, 25, 10]
['Apple', 'Orange', 'Zebra']
['Zebra', 'Orange', 'Apple']
['Apple', 'Zebra', 'Orange']

List of Programs


Sample Programs


Python Database Connection


Python Flask


Python Tkinder Tutorial


--> List in Python

List in Python


Lists are used to store multiple items in a single variable.To separate two items, you use a comma ( , ) . List uses the square brackets [ ]. Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage.

  • Sequence Type
  • Element of the list can access by index
  • They are mutable

This program demonstrates various built-in functions and methods that can be used with lists in Python.

  • The first block of code shows how to create a list and access its elements using indexing. It also shows how to change the value of an element in a list using indexing.
  • The second block of code shows how to create a list with different data types and how to access the elements of the list and their types.
  • The third block of code demonstrates how to use the clear() method to remove all elements from a list, the copy() method to create a copy of a list, the count() method to count the number of occurrences of an element in a list, the index() method to find the index of an element in a list, the len() function to find the length of a list, the max() and min() functions to find the maximum and minimum element in a list, the pop() method to remove an element from a list using an index, and the remove() method to remove an element from a list using its value.
  • The fourth block of code shows how to use the append() method to add elements to a list, the extend() method to add elements from another list, and the insert() method to insert an element at a specific index in a list.
  • The fifth block of code demonstrates how to use the range() function to create a list of numbers, the list() function to convert a string to a list, and the sort() method to sort the elements of a list in ascending or descending order. It also shows how to use the key parameter to sort the elements based on a specific key and the reverse parameter to sort the elements in descending order.

Source Code

# List in Python
"""
Sequence Type
Mutable
a[5]
a={1,2,3,4,5}
a[0]
"""
a = [1, 2, 3, 4, 5]
print(a)
print(type(a))
a[0] = 100
print(a)
print("Slicing")
print(a[1])
print(a[-1])
print(a[0:3])
print(a[2:])
print(a[:3])
print("-----------------------------")
a = [1, True, 'Ram', 2.5, [1, 2, 3, 4]]
print(a)
print(type(a))
print(a[0], " type is ", type(a[0]))
print(a[1], " type is ", type(a[1]))
print(a[2], " type is ", type(a[2]))
print(a[3], " type is ", type(a[3]))
print(a[4], " type is ", type(a[4]))
print(a[4][1])
print("-----------------------------")
a = [10, 25, 35, 45]
print(a)
a.clear()
print(a)
a = [10, 25, 35, 45]
b = a.copy()
print(b)
a = [10, 25, 35, 45, 25, 4, 25]
print(a.count(25))
print(a.index(25))
print(len(a))
print(max(a))
print(min(a))
print(a)
a.pop(0)  # remove Element using index
print(a)
a = [10, 25, 35, 45, 25, 4, 25]
a.remove(25)  # Values
print(a)
print("-----------------------------")
names = ["Ram"]
print(names)
names.append("Sam")
names.append("Ravi")
names.append("Kumar")
print(names)
name2 = ["Sara", "Anitha"]
names.extend(name2)
print(names)
names.insert(0,"Suriya")
print(names)
print("-----------------------------")
print(list(range(5)))
print(list("Tutorjoes"))
a=[10,50,100,25,85]
print(a)
a.sort()
print(a)
a.sort(reverse=True)
print(a)
a=["Orange","Apple","Zebra"]
a.sort()
print(a)
a.sort(reverse=True)
print(a)
a=["Orange","Apple","Zebra"]
a.sort(key=len)
print(a)

To download raw file Click Here

Output

[1, 2, 3, 4, 5]

[100, 2, 3, 4, 5]
Slicing
2
5
[100, 2, 3]
[3, 4, 5]
[100, 2, 3]
-----------------------------
[1, True, 'Ram', 2.5, [1, 2, 3, 4]]

1  type is  
True  type is  
Ram  type is  
2.5  type is  
[1, 2, 3, 4]  type is  
2
-----------------------------
[10, 25, 35, 45]
[]
[10, 25, 35, 45]
3
1
7
45
4
[10, 25, 35, 45, 25, 4, 25]
[25, 35, 45, 25, 4, 25]
[10, 35, 45, 25, 4, 25]
-----------------------------
['Ram']
['Ram', 'Sam', 'Ravi', 'Kumar']
['Ram', 'Sam', 'Ravi', 'Kumar', 'Sara', 'Anitha']
['Suriya', 'Ram', 'Sam', 'Ravi', 'Kumar', 'Sara', 'Anitha']
-----------------------------
[0, 1, 2, 3, 4]
['T', 'u', 't', 'o', 'r', 'j', 'o', 'e', 's']
[10, 50, 100, 25, 85]
[10, 25, 50, 85, 100]
[100, 85, 50, 25, 10]
['Apple', 'Orange', 'Zebra']
['Zebra', 'Orange', 'Apple']
['Apple', 'Zebra', 'Orange']

List of Programs


Sample Programs


Python Database Connection


Python Flask


Python Tkinder Tutorial