List in Python with Examples

List in Python

Hello Techies,

This blog is about “List in Python”. A list is a data type that allows you to store other data types in it. So in this blog, we will learn about the usage of the list in Python.

In my previous blog, I covered built-in datatypes in Python. Check it out too.

Let’s start and check, How to use List in Python step by step.

Below are the Key Points of List in Python that we are covering in this blog.

  1. Python List (Introduction)
  2. Create List in Python
  3. Index in List Python
  4. Accessing List Element
  5. Modifying List Element
  6. Delete or Remove elements from List
  7. Python List Comprehension
  8. Python List Slice
  9. Python List Range
  10. Python List Methods

#1. Python List (Introduction)

  • A list represents a group of elements.
  • Lists are very similar to an array but there is a major difference, an array can store only one type of elements whereas a list can store different types of elements.
  • List are mutable so we can modify its element.
  • A list can store different types of elements that can be modified.
  • The list is dynamic which means size is not fixed.
  • The list is represented using a square bracket [].
  • Example: data = [1, -89, “test”, “TechPlusLifeStyle”]

#2. Create List in Python

A list is similar to an array that contains a group of elements or items.

Syntax

list_name = [item1, item2, item3, ...]

Example

data = [1, -89, "test", "TechPlusLifeStyle"]

#3. Index in List Python

An index represents the position number of a list’s elements. The index starts from 0 onwards and is written inside square braces.

Example:- data = [1, -89, “test”, “TechPlusLifeStyle”]

This index is represented in 2 ways Positive and negative index. The Positive index starts from the 0 positions while in Negative indexing -1 refer last element, -2 second last element and so on.

Positive Index

IndexData
[0]1
[1]-89
[2]test
[3]TechPlusLifeStyle

Negative Index

IndexData
[-1]TechPlusLifeStyle
[-2]test
[-3]-89
[-4]1

#4. Accessing List Element

There are several ways in which we can access the elements in a list. Let’s check the above example which described in 3rd point.

Example for Positive indexing

data = [1, -89, "test", "TechPlusLifeStyle"]
print(data[0])
print(data[1])
print(data[2])
print(data[3])

Output

1
-89
test
TechPlusLifeStyle

Example for Negative indexing

data = [1, -89, "test", "TechPlusLifeStyle"]
print(data[-4])
print(data[-3])
print(data[-2])
print(data[-1])

Output

1
-89
test
TechPlusLifeStyle

#5. Add or Update List Element

List are mutable so we can modify its element.

We can use the append() method to add any new elements to the list or add several elements using the extend() method.

data = [1, -89, "test", "TechPlusLifeStyle"]
data.append(2)
print(data)
data.extend([1,5,8])
print(data)

Output

[1, -89, "test", "TechPlusLifeStyle", 2]
[1, -89, "test", "TechPlusLifeStyle", 2, 1, 5, 8]

We can use the = operator to modify any element in the list.

data = [1, -89, "test", "TechPlusLifeStyle"]

data[0] = 2
print(data)

Output

[2, -89, "test", "TechPlusLifeStyle"]

#6. Delete or Remove elements from List

To delete one or more elements from the list using Keyword del. It may even delete the entire list.

# deleting single element
data = ['p', 'w', 'o', 'a', 'g', 'e', 'm']
del data[2]
print(data)

# deleting multiple elements
del data[1:5]
print(data)

# deleting the entire list
del data

# Error: List not defined
print(data)

Output

['p', 'w', 'a', 'g', 'e', 'm']
['p', 'm']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'data' is not defined

We can use the remove() method to remove a given item or the pop() method to remove an item from a given index.

The pop() method removes and returns the last item if no index is provided. We can also use the clear() method to clear the list.

my_list = ['a','b','c','d','e','f','g']

my_list.remove('c')
print(my_list)

print(my_list.pop(1))
print(my_list)

print(my_list.pop())
print(my_list)

my_list.clear()
print(my_list)

Output

['a','b','d','e','f','g']
b
['a','d','e','f','g']
g
['a','d','e','f']
[]

#7. Python List Comprehension

  • A concise way to create a new list from an existing list in Python is list comprehension.
  • The comprehension in the list has an expression followed by a statement in square brackets.

Here is an example,

even_number_list = [x for x in range(10) if x%2==0]
print(even_number_list)

Output

[0, 2, 4, 6, 8]
Interview question

Q: Write a program to generate Even number using list comprehension in   
     python

Ans: Check the above program for this question

#8. Python List Slice

Slice Operator: Using (colon) we can enter the range of items in the list.

my_list = [1,2,3,4,5,6,7,8,9]

# elements 2nd to 5th
print(my_list[1:5])

# elements beginning to 6th
print(my_list[:-3])

# elements 3rd to end
print(my_list[2:])

# elements beginning to end
print(my_list[:])

Output

[2, 3, 4, 5]  # output of my_list[1:5]
[1, 2, 3, 4, 5, 6]  # output of my_list[:-3]
[3, 4, 5, 6, 7, 8, 9]  # output of my_list[2:]
[1, 2, 3, 4, 5, 6, 7, 8, 9]   # output of my_list[:]

#9. Python List Range

Most of the time you want to make a list with continuous values in the range of 10. Let us discuss how to create a list using the range() function.

Syntax

range(start, stop[, step])
print(list(range(10)))
print(list(range(2, 10)))
print(list(range(2, 10, 2)))

Output

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]   # output of list(range(10))
[2, 3, 4, 5, 6, 7, 8, 9]   # output of list(range(2, 10))
[2, 4, 6, 8]   # output of list(range(2, 10, 2))

#10. Python List Methods

The methods available with the list objects in Python programming are in the table below.

Method NameDescription
append()Add an element at the end of the list
extend()Add all the elements of the list to the second list
insert()Insert an object into the defined index
remove()Removes items from the list
pop()Removes and returns an element from a given index
clear()Removes all items from the list
index()Returns the index of the first matching item
count()Calculate the number of items passed as arguments and return the count
sort()Sort the listed items in ascending order
reserve()Reverse order of items in the list
copy()Returns a shallow copy of the list

Some examples of Python List methods

my_list = [4, 8, 1, 6, 8]

# index() method
print(my_list.index(8))

# count() method
print(my_list.count(8))

# sort() method
my_list.sort()
print(my_list)

# reverse() method
my_list.reverse()
print(my_list)

Output

1
2
[1, 4, 6, 8, 8]
[8, 8, 6, 4, 1]

I hope you understand the use of “List in Python”. If you still have any doubts please comment below.

Check out the official site for more details on “List in Python”.

Leave a Comment