Introduction to Python List with Practical Example

In this article you will learn about Python List.

Python List – List is a Python in-built function and it contains four type of data types.

The data types are following as –

1. List – It is ordered collection and contains changeable elements. It also allows duplicate elements (repetition of elements).

2. Tuple – It is ordered collection and contains unchangeable elements. It also allows duplicate elements (repetition of elements).

3. Set – It is collection of unordered and unindexed elements. It doesn’t allow duplicate elements.

4. Dictionary – It is unordered collection and contains unindexed elements. It doesn’t allow duplicate elements.

Let’s begin with first Python List –

List – It is ordered collection and contains changeable elements. In Python list are defined with square brackets [].

Example 1- Creation of a list.

x = ['Hello', 'Python', 'world']                                                                                               
print(x)
python-list

Access elements – In Python, list elements can get using index number of elements.

Index number – In Python, Index number starts from 1.

HELLO
12345
Index number

Example 2- Get the element of position 2.

x = ['Hello', 'Python', 'world']
print(x[2])
python-list

Note: – In beginning index number starts from 0.

Negative indexing – It defined as indexed number of elements from end starting with -1 for last first element and -2 for last second element and so on.

HELLO
-5-4-3-2-1
Negative Indexing

Example 3- Get the element of position 3 from end.

x = ['Hello', 'Python', 'world']
print("Element of position 3 from end is:" , x[-3])
python-list

Note: In ending index number starts from -1.

Range of indexes – It specified range of index number to get particular element from start to end.

Specifying range returns new list of specified elements.

Example 4- Get particular elements from index number 1 to 2.

x = ['Welcome', 'in', 'programming', 'language']
print("Element from index number 1 to 3 is:" , x[1:3])
python-list

Note: Starting index number (1) is included but ending number (3) is excluded.

Example 5- Start search from beginning to till particular element.

x = ['Welcome', 'in', 'programming', 'language']
print(x[:3])
python-list

Example 6- Start search from any elements or mid to get elements till end.

x = ['Welcome', 'in', 'programming', 'language']
print(x[1:])
python-list

Range of Negative Indexes – Start search from end to get particular elements from start to end.

Example 7- Start search from end and get particular elements  from start to end.

x = ['Welcome', 'in', 'programming', 'language']
print(x[-4:-1])

Negative indexing – It defined as indexed number of elements from end starting with -1

python-list

Change Item Value – It defined as change of particular elements by describing index number of changeable elements.

Example 8- Change element ‘language’ into ‘Python’.

x = ['Welcome', 'in', 'programming', 'language']
x[2] = 'Python' 

print(x)
python-list

Now we are going to learn some important functions of list.

Loop in list – It is possible to loop though list by using ‘for’ loop.

Example 9- Use of ‘for’ loop to looping through list.

z = ['Welcome', 'in', 'programming', 'language']
for x in z:
    print(x)
python-list

Note:  We will learn more about ‘for’ loop later in detail.

Check element exits – To check particular element exits, use ‘in’ keyword.

Example 10- Check if element ‘programming’ exits.

x = ['Welcome', 'in', 'programming', 'language']
print('programming' in x)
python-list

Length of list – To get length of list use ‘len’ function.

Example 11- Use len() function to get length.

x = ['Welcome', 'in', 'programming', 'language']
print(len(x))
python-list

Add element – To add an element at the end position of list, use append() function.

Example 12- Use of append() method to add element at the end position.

x = ['Welcome', 'in', 'Python']
print('before adding:' , x)

x.append('language')
print('After adding:' , x)
python-list

To add an element at the specified index number, use insert() method.

x = ['Python', 'Java', 'HTML']
x.insert(2, 'JavaScript')

print(x)
python-list

Remove elements – To remove any elements from list, use remove() method. There are so many other functions too to remove elements. They are follows as ‘pop()’, ‘del’ and clear.

Example 13- Use of remove() method to remove any element.

x = ['Python', 'Java', 'HTML']
x.remove('Java')

print(x)
python-list

pop() method – By default it removes last element.

Example 14- Use of pop() method to remove last element.

x = ['Python', 'Java', 'HTML']
print("before removing:" , x)
x.pop()

print("After removing:" , x)
python-list

del() method – If index number is specified it deleted that particular indexed number element, otherwise delete whole list.

Example 15- Use of del() method to delete particular element.

x = ['Python', 'Java', 'HTML']
print('before deleting:' , x)

del x[1]
print("after deleting", x)
python-list

Example 16- Use of del method to delete whole list.

x = ['Python', 'Java', 'HTML']
print('before deleting:' , x)

del x print("after deleting" , x)
python-list

clear() method – It left an empty list.

Example 17- Use of clear() method to empty list.

x = ['Python', 'Java', 'HTML']
print("before clearing:" , x)

x.clear()
print("after clearing:" , x)

Copy list – To copy a list Python has two function-

copy() method – It is used to copy a list.

list() method – It is used to copy a list.

Example 18- Use of copy() method to copy a list.

x = ['Python', 'Java', 'HTML']
z = x.copy()

print(x)
python-list

Example 19- Use of list() method to copy a list.

x = ['Python', 'Java', 'HTML']
z = list(x)

print(z)
python-list

Join list – In Python, list can be easily joined or concatenated by using + operator.

Example 20- Concatenation of two lists.

x = [1,2,3]
z = [4,5,6]

print(x+z)
python-list

Example 21- Joining two lists by appending one list elements to another list one by one.

x = [1,2,3]
z = [4,5,6]

for c in z:
    x.append(c)
print("after appending:" , x)
python-list

extend() method – It adds element of one list into the another list.

x = [1,2,3]
z = [4,5,6]
x.extend(z)

print(x)
python-list

The list() Constructor – It uses to construct or make new list.

Example 22- Use of constructor to make new list.

x = list((1,2,3,4,5))
print(x)
python-list

If you find anything incorrect in above discussed topic and have any further question, please comment down below.

Like us on

2 thoughts on “Introduction to Python List with Practical Example”

Leave a Comment

Stay in the loop

codingstreets