Python Lists

Python Lists, Creating Lists, Accessing Values in Lists, Updating Lists, Delete List Elements, and Built-in List Functions and Methods.

Python Lists

The sequence is the most basic data structure in Python, Each element of a sequence is assigned a number – its position or index. The first index is zero, the second index is one, and so forth.

Python has six built-in types of sequences, but the most common ones are Lists and Tuples.
—————————–
The list is the most versatile datatype available in Python which can be written as a list of comma-separated values (items) between square brackets. The important thing about a list is that items in a list need not be of the same type.

Example:

list1 = [10, 20, 30, 40, 50]
list2 =[19.23, 20.45, 5.678, 9.86]
list3 = [“Java”, “Python”, “JavaScript”, “VBScript”]
list4= [True, False, False, None]
list5 =[“Python”, “Java”, 10, 10.234, True]

Print Lists

print (list1)
print (list2)
print (list3)
print (list4)
print (list5)

Print a List line by Line

list1 =[“Python”, “Java”, 10, 10.234, True]

print(list1)
print(“”)

for i in list1:
print (i)

1. Accessing Values in Lists

To access values in lists, use the square brackets for slicing along with the index.

Examples:

abc = [“Selenium”, “UFT”, “SilkTest”, 100, 200, 300]
print (abc)
print (abc[1])

2. Updating Lists

You can update single or multiple elements of lists by giving the slice on the left-hand side of the assignment operator, and you can add elements in a list with the append() method.

Example:

list = [‘physics’, ‘chemistry’, 1997, 2000]

print (list)
print (list[2])
list[2] = 9999
print (list);
print (list[2])

3. Delete List Elements

To remove a list element, you can use either the del statement if you know exactly which element(s) you are deleting or the remove() method if you do not know.

Example:

list1 = [‘physics’, ‘chemistry’, 1997, 2000]

print (list1)
del (list1[2])
print (list1)

4. Built-in List Functions & Methods / Operations on Python Lists

i. len()
Gives the total length of the list.

Example:
list = [10, 20, 30, 40, 50]

print (len(list))

ii. max()
Returns item from the list with max value.

Example:

list = [10, 20, 30, 40, 50]

print (max(list))

iii. min()

Returns item from the list with min value.

Example:

list = [10, 20, 30, 40, 50]

print (min(list))
——————————-
Methods with Description

i. append() method

Appends object obj to list

Example:
list = [10, 20, 30, 40, 50]

print(list)
list.append(70)
print(list)

ii. remove() method
Removes an element from a list by element value.

Example:

list = [10, 20, 30, 40, 50]

print(list)
list.remove(50)
print(list)

iii) reverse() method
Reverses elements of list in place

Example:

list = [10, 20, 30, 40, 50]

print(list)
list.reverse()
print(list)

Python Step by Step Tutorial

1. Introduction to Python Programming Language

2. Download and Install Python

Python Environment Setup (Using PyCharm IDE)

3. Python Language Syntax

4. Python Keywords and Identifiers

5. Comments in Python

6. Python Variables

7. Python Data Types

8. Python Operators

9. Python Conditional Statements

10. Python Loops

11. Python Branching Statements

12. Python Numbers

13. String Handling in Python

14. Python Data Structures – Lists

15. Python Data Structures – Sets

16. Python Data Structures – Tuples

17. Python Data Structures – Dictionaries

18. Python User Defined Functions

19. Python Built-in Functions

20. Python Modules

21. Python User Input

22. File Handling in Python

23. Python Date and Time

24. Python Exception Handling

25. Python Regular Expressions

26. Python Object-Oriented Programming

27. Inheritance in Python

28. Polymorphism in Python

29. Abstraction and Encapsulation in Python

Python Vidoes PlayList
Follow me on social media: