Python Lists With Program Examples

Spread the love

Python lists are powerful data structures, we can define by using []. We can store numbers, strings, float numbers in this list. We can do append,insert, extend, pop, delete methods using lists.

First we need to learn how create a empty list, string list, float list then we can go further.

How to create a empty list:

We can create a list by using [] operator.

List = [] print("Blank list") print(List)

Output: 

Blank list
[]

How to create a list with values:

Here i am create a list with values and printing that list.

List = [1, 2, 4, 4, 3, 3, 3, 6, 5] print("\nList with the use of Numbers: ") print(List)

Output:

List with the use of Numbers:
[1, 2, 4, 4, 3, 3, 3, 6, 5]

How to create a string list:

Here i am create a list with string values, by specifying string values with quotes and commas

#list with strings List = ["Dog", "fish", "Cat"] print("\nList containing with string values: ") print(List)

Output:

List containing with string values:
['Dog', 'fish', 'Cat']

How to create a float list:

List = [1.5,2.5,3.5,7.8,9.5] print("\nList containing with float values: ") print(List)

Output:

List containing with float values:
[1.5, 2.5, 3.5, 7.8, 9.5]

How to Create a Multi Variable List :

#mutli variable list List = [1,2,'cat',3.5,7.8,9.5] print("\nList containing with multitype values: ") print(List)

Output:

List containing with multi-type values:
[1, 2, 'cat', 3.5, 7.8, 9.5]

List Operations with Append, Pop, Delete, Extend etc.

If you want to change list data, we need to use list operations with desired inbuilt functions.

How to Add Elements to the list:

We can add elements to the list by using append(), extend() and insert functions.

List.append() function:

This function used to append a new value to the list.

Syntax: list.append(‘value’)

#program to understand append function in lists.

List =[1,2,3,4] List.append(5) print("\nList after appending value ") print(List)

Output:

List after appending value
[1, 2, 3, 4, 5]

List.append():

This function used to add a new value for specific index to the list.

Syntax: list.insert(position, value)

List =[1,2,3,4] List.insert(2,5) print("\nList appending value at starting of the list ") print(List)

Output:

List appending value at starting of the list 
[1, 2, 5, 3, 4]

How to remove element from the list:

We can remove list elements by using pop()  and remove() functions.

List.pop() function:

This function help to remove the last element from the list.

List =[1,2,3,4] List.pop() print("\nRemoving last element from list") print(List)

Output:

Removing last element from list
[1, 2, 3]

List.remove():

list.remove() function helps to remove specific item from the list.

List =[1,2,3,4] List.remove(2) print("\nRemoving specific element from list") print(List)

Output:

Removing specific element from list
[1, 3, 4]

#Example 2 on Remove list items

List =[1,2,3,4] del List[0] print("\nRemoving starting element from list") print(List)

Output:

Removing starting element from list
[2,3,4]

How to delete all elements from list.

List.clear()

List.clear() function helps to delete all items from the list,

List =[1,2,3,4] List.clear() print("\nClear the list") print(List)

Output:

Clear the list
[]

How to copy one list to another list ?

List.copy() function:

List.copy() function helps to create a new list by copying all elements from the old list.

del List List = [1, 2, 4, 4, 3, 3, 3, 6, 5] List2 = List.copy() print("\nCopy the list 1 to list 2") print(List2)

Output:

Copy the list 1 to list 2
[1, 2, 4, 4, 3, 3, 3, 6, 5]

How to count specific element from the list ?

List.count():

This function helps to count the specific element from the list.

List = [1, 2, 4, 4, 3, 3, 3, 6, 5] Count = List.count(4) print("\nCounting of number of times that values appears in the list") print(Count)

Output:

Counting of number of times that values appears in the list
2

How to Check index of the list element:

List.index() Function:

list.index() function helps to find the element by using index.

List = [1, 2, 4, 4, 3, 3, 3, 6, 5] index = List.index(4) print("\nTo check Index value of value") print(index)

Output:

To check Index value of value
2

How to Reverse the list elements:

List.reverse():

This function helps to reverse a list.

List = [1, 2, 4, 4, 3, 3, 3, 6, 5] List.reverse() print("\n Reverse of the list", List)

Output:

Reverse of the list
[5, 6, 3, 3, 3, 4, 4, 2, 1]

How to Sort List Elements :

List.sort() function:

This function help us to sort a list using ascending order and descending order.

List = [1, 2, 4, 4, 3, 3, 3, 6, 5] print(List) List.sort() print("\n sorting of the list Ascending") print(List) List.sort(reverse=True) print("\n sorting of the list Descending") print(List)

Output:

sorting of the list Ascending
[1, 2, 3, 3, 3, 4, 4, 5, 6]
sorting of the list Descending
[6, 5, 4, 4, 3, 3, 3, 2, 1]
List = [1, 2, 4, 4, 3, 3, 3, 6, 5] print(List) List2= [4,5,6] List.extend(List2) print("\n Extented list") print(List)

Output:

Extented list
[1, 2, 4, 4, 3, 3, 3, 6, 5, 4, 5, 6]

admin

admin

Leave a Reply

Your email address will not be published. Required fields are marked *