Python

Python List Example

Hello readers, in this tutorial, we will learn about List in Python programming language.

1. Introduction

The list data structure in python programming is a –

  • A data structure that can store multiple data at once (in other words it is a comma separate items between the square brackets and the items can be of the different or same type)
  • It is an ordered collection
  • Each element in a list have a distinct place in the sequence and will not eradicate the duplicates
  • It is mutable in nature (i.e. we can add and remove the entries

1.1 Setting up Python

If someone needs to go through the Python installation on Windows, please watch this link. To start with this tutorial, I am hoping that readers at present have the python installed.

2. List in Python Programming Tutorial

I am using JetBrains PyCharm as my preferred IDE. Readers are free to choose the IDE of their choice.

2.1 How to create a list and accessing values?

Creating a list is as simple as putting different comma-separated values in the square brackets. To access values in the list we can either use the loop or the print(…) function directly. Let us understand this with the help of a code snippet.

Creating a List and Accessing it

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
# Creating a car list
cars = [
    'BMW',
    'Audi',
    'Mercedes',
    'Honda',
    'Toyota',
    'Ferrari',
    'Tesla'
]
# Accessing the values from the list
# Approach1 -
print('Approach #1= ', cars)
print('=====')
# Approach2 -
print('Approach #2')
for car in cars:
    print('Car name=', car)

If everything goes well the following output will be shown in the IDE console.

01
02
03
04
05
06
07
08
09
10
Approach #1=  ['BMW', 'Audi', 'Mercedes', 'Honda', 'Toyota', 'Ferrari', 'Tesla']
=====
Approach #2
Car name= BMW
Car name= Audi
Car name= Mercedes
Car name= Honda
Car name= Toyota
Car name= Ferrari
Car name= Tesla

2.2 Adding an element to List

To add an element to a list we have two methods in python programming i.e. –

  • The append(…) method which will add an element at the end
  • The insert(…) method which will add an element at a given index

Let us understand this with the help of a code snippet.

Adding an element

1
2
3
4
5
# 3. Adding an element to the list and accessing it.
cars.append('Tata')  # Add an element at the end of the list
# Add an element at the given index (& shifts the elements greater than index one step to the right)
cars.insert(2, 'Kia')  # (Insert 'Kia' at position no. 2)
print('New cars list= ', cars)

If everything goes well the following output will be shown in the IDE console.

1
New cars list=  ['BMW', 'Audi', 'Kia', 'Mercedes', 'Honda', 'Toyota', 'Ferrari', 'Tesla', 'Tata']

2.3 Deleting an element from List

To remove an element from a list we have two methods in python programming i.e.

  • The remove(…) method will search the first occurrence of the given element in the list and remove it
  • The pop(…) method will remove the last element of the list

Let us understand this with the help of a code snippet.

Adding an element

1
2
3
4
5
6
# 4. Removing an element from the list.
cars.remove('Mercedes')
print('remove() method = ', cars)
cars.pop()
print('pop() method = ', cars)

If everything goes well the following output will be shown in the IDE console.

1
2
remove() method =  ['BMW', 'Audi', 'Kia', 'Honda', 'Toyota', 'Ferrari', 'Tesla', 'Tata']
pop() method =  ['BMW', 'Audi', 'Kia', 'Honda', 'Toyota', 'Ferrari', 'Tesla']

2.4 Some basic functions

  • The len(…) method prints the length of the list
  • The sort(…) method sorts the elements of a list
  • The reverse(…) method reverses the list

Let us understand this with the help of a code snippet.

Basic functions

01
02
03
04
05
06
07
08
09
10
11
12
# 5. Basic functions over list
# 5(a). Print length of a list
print('Length of list= ', len(cars))
# 5(b). Sort the elements of a list
cars.sort()
print('Sorted list= ', cars)
# 5(c). Reversed the list
cars.reverse()
print('Reversed list= ', cars)

If everything goes well the following output will be shown in the IDE console.

1
2
3
4
5
Length of list=  7
Sorted list=  ['Audi', 'BMW', 'Ferrari', 'Honda', 'Kia', 'Tesla', 'Toyota']
Reversed list=  ['Toyota', 'Tesla', 'Kia', 'Honda', 'Ferrari', 'BMW', 'Audi']

That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share!

3. Summary

In this tutorial, we learned about the List data structure in python programming.

You can download the sample class from the Downloads section.

4. Download the Project

This was an example of a List data structure in python programming.

Download
You can download the full source code of this example here: Python List Example

Yatin Batra

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button