Python Lists

Python list; Through this tutorial, i am going to show you what is lists in Python and how to adding or removing elements from python lists and so on.

Python Lists

  • What is list in python?
  • How to create a list?
  • How to access elements from a list?
  • Python find the length of list
  • Add item to list python
  • Add item to list at specific index python
  • Remove item from list python
  • Python remove specific item from list
  • python copy list of lists
  • python join two lists
  • Lists Methods Python

What is list in python?

The lists in python are Collections of items. which is ordered and changeable. The lists are defined by having values between square brackets [ ].

How to create a list?

To create a list in Python, place all the items within [] brackets and separate the items with commas.

A Python list can contain elements of different data types. Such as integer, float, string, etc.

python create empty list

Create an empty list in python; as shown below:

# empty list
my_list = []

Python create list with numbers

Create a list in Python, in which list will be Numbers; as shown below:

# list of integers
my_list = [1, 2, 3]

Python create list of string numbers

Create another list which will contain numbers, strings, etc; as shown below:

# list with mixed datatypes
my_list = [1, "Hello", 3.4]

How to access elements from a list?

Create lists in Python with different data types. And get items / elements from lists.

Python access list element by index

# list of integers
my_list = [10, 20, 30]
print(my_list[1])

As there is a list, from this list you have access to the items according to the index.

Output

20

Python access list element by negative index

Negative indexing means beginning from the end, -1 refers to the last item, -2 refers to the second last item etc.

# list of integers
my_list = [10, 20, 30]
print(my_list[-1])

Output

30

Python list get element by index range

You can get items in the range from the list. For this, the start and end range values have to be passed:

# list of integers
my_list = [10, 20, 30, 40, 50, 60, 70, 80, 90]
print(my_list[2:6])

Output

 [30, 40, 50, 60]

Python find the length of list

If you want to find/get the list’s length, then you have to use the Python len() function.

You can see the below example how the list is find by length:

# list of integers
my_list = [10, 20, 30, 40, 50, 60, 70, 80, 90]
print(len(my_list))

Output

9

Add item to list python

Now, you will learn how to add items to the list in Python.

The append () function is used to add items to the Python list. And the item is added to the end of the list.

# list of integers
my_list = [10, 20, 30, 40, 50, 60, 70, 80, 90]
my_list.append(100)
print(my_list)

Output

[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

Add item to list at specific index python

If you want to add an item to the specific index of the list in Python, then you have to use insert() for it.

# list of integers
my_list = [10, 20, 30, 40, 50, 60, 70, 80, 90]
my_list.insert(1, 5)
print(my_list)

Output

[10, 5, 20, 30, 40, 50, 60, 70, 80, 90]

Remove item from list python

By now you have learned how to add and access items in the list. Now you will learn how to delete/remove items from the Python list.

The remove() method is used to remove the first occurrence item with a specified value on python lists.

To delete/remove items from the Python list, use the function remove() of Python.

You can see the example below. How to remove/delete items from the list:

# list of integers
my_list = [10, 20, 30, 40, 50, 60, 70, 80, 90]
my_list.remove(90)
print(my_list)
 [10, 20, 30, 40, 50, 60, 70, 80] 

python remove specific item from list

If you want to remove a specific item from the list of Python list. So for this you have to use pop () function.

You can see the example below. Let’s use the Python pop() function to remove the item from the specific index:

# list of integers
my_list = [10, 20, 30, 40, 50, 60, 70, 80, 90]
my_list.pop(2)
print(my_list)

Note:- If you do not pass the index inside the pop() function, it will remove the value of last indexes in the python list.

Output

 [10, 20, 40, 50, 60, 70, 80, 90] 

If you want to remove items from the specific index of Python list, you can also use del keyword for this

Let’s take an example to understand better how to use the del keyword and remove the item from the list:

# list of integers
my_list = [10, 20, 30, 40, 50, 60, 70, 80, 90]
del my_list[3]
print(my_list)

In the example given, you have removed the item that was on the 3rd index in python lists. You can see the output below:

Output

 [10, 20, 30, 50, 60, 70, 80, 90]

python copy list of lists

In Python, you want to copy one list to another list. So for this, you have to use the copy () function of Python.

The copy () function copies one list into another list.

# list of integers
my_list = [10, 20, 30, 40, 50, 60, 70, 80, 90]
blist= my_list.copy()
print(blist)

Output

 [10, 20, 30, 40, 50, 60, 70, 80, 90]

python join two lists

If you want to join two lists in Python. So you can use its + operator. This will join your two lists.

Now you take an example. In this example, two lists are taken, one is a list of string data types and the other is a list of integer data types. And will use the + operator to connect to it:

list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]
list3 = list1 + list2
print(list3)

Output

 ['a', 'b', 'c', 1, 2, 3] 

Lists Methods Python

Below are more in-built methods for working with lists in Python:

MethodDescription
append()Adds an element at the end of the list
clear()Removes all the elements from the list
copy()Returns a copy of the list
count()Returns the number of elements with the specified value
extend()Add the elements of a list (or any iterable), to the end of the current list
index()Returns the index of the first element with the specified value
insert()Adds an element at the specified position
pop()Removes the element at the specified position
remove()Removes the item with the specified value
reverse()Reverses the order of the list
sort()Sorts the list

Recommended Python Tutorials

Recommended:-Python Data Types
Recommended:-Functions in Python
Recommended:-Python Modules

Leave a Comment