Python Lists

Hey folks, in today’s tutorial we will explain on python lists. In this tutorial we will discuss how to create lists, finding the size of the list, adding elements to the list, accessing elements from the list, removing elements from the list, slicing the list, list comprehension, list methods and operations on list. But before that let’s explain what are lists.

Lists are just like dynamic sized arrays, declared in other languages. A single list may contain various valid data types like strings, integers and may also have objects. Lists are mutable, hence they can be changed or manipulated after creation. Lists in python are ordered with a particular count, its elements are indexed and they start counting from 0, 1, 2, 3…this means that the first element has a value of 0. Each single element in the list has its definite place which allows duplicating of elements in the list, with each element having its own distinct place and credibility.

In the following section, we will discuss lists in details on how create and manipulate giving out relevant examples.

How To Create List In Python

When creating list in python you just places the sequence in between the square brackets[]. Like can be created without using built-in functions unlike sets, look into the following example;

case 1;
#creating list in python

list = []
print ("blank list:")
print (list)


case 2;
#creating a list of numbers 

list = [56, 82, 53]
print("list of numbers:")
print(list)


case 3;
#creating a list of strings using index

list = ["learntechphilloh", "program", " free for all"]
print("list of items:")
print(list[1])
print(list[2])


case 4;
#creating a multi-dimensional list

list =[['learntechphilloh', 'program'], ['free for all']]
print("multi-dimensional list:")
print(list)

It will output the following;

blank list:
[]

list of numbers:
[56, 82, 53]

list of items:
program
free for all

multi-dimensional list:
[[‘learntechphilloh’, ‘program’], [‘free for all’]]

Creating a list with multiple distinct or duplicate elements

Above we just discussed how you can create a simple list of numbers, strings or even multi-dimensional. In this part we will learn how you can create list with duplicate values each with a distinct positions and therefore, multiple distinct or duplicate values can be passed as a sequence at a time when creating the list. Look at the example below;

case 1;
#creating list with numbers that are duplicate

list =[3,4,5,7,9,9,9,1,2,2]
print(list)


case 2;
#creating list with mixed type of values

list =[6, 7, 9, 'learntechphilloh', 'program', 4, 'free']
print(list)

It will output the following;

[3,4,5,7,9,9,9,1,2,2]


[6,7,9,’learntechphilloh’,’program’,4,’free’]

Finding the size of the list

To find the size of the list is to determine the number of elements in the list. We use len() function to find the length of the list, either can be numbers or strings enclosed in square brackets. Look at the examples below;

case 1;

list =[]
print(len(list))


case 2;

list =[12, 45,79,71]




It will output the following;
0
4

How To Add Elements To A List

In this part we will discuss how you can add elements to a list, there are various ) in which you can add elements to a list that is by use of append() method, insert() method and extend() method. We will explain each giving out examples.

a) How to add elements to a list by use of append() method

You can add elements to a list by use of append() function. In this case, only one item is added to the list at a time by using this method. Tuples can also be added to the list by use of this method since tuples are immutable, you can also add lists to an existing list by use of this method. Look at the examples below;

case 1;
#fist you create a blank list

list =[]
print(list)

case 2;
#Now add elements to the list

list.append(7)
list.append(8)
list.append(5)
print(list)


case 3;
#Adding tuples to the list

list.append(('8,24'))
print(list)


case 4;
#Adding elements to the list using iterator

for x in range(2,5):
 list.append(x)
print(list)


case 5;
#Adding a list to a list

list1 =['learntechphilloh', 'program']
list.append(list1)
print(list)

It will output the following;

[]

[7,8,5]

[7,8,5,(8,24)]

[7,8,5,2,3,4,(8,24)]

[7,8,5,2,3,4,(8,24),[‘learntechphilloh’,’program’]]