Python List: The Complete Guide

0
724
Python List Example | List In Python Tutorial Explained

Python list is the most versatile datatype available. An important thing about a list is that items in a list need not be of the same type. There are four collection data types in the Python programming language:

  1. The list is the collection that is ordered and changeable. It allows duplicate members.
  2. A tuple is a collection that is ordered and unchangeable. It allows duplicate members.
  3. Set is the collection that is unordered and unindexed — no duplicate members.
  4. Dictionary is a collection that is unordered, changeable, and indexed. No duplicate members.

In this example, we will talk about Python List. A list is a collection that is ordered and changeable. In Python, lists are written with square brackets.

Python List

Python list is a built-in data type with comma-separated values (elements) between square brackets. The list may contain duplicate values with their distinct positions; hence, multiple distinct or duplicate values can be passed as a sequence at the time of list creation.

Let’s create a list that is as simple as putting different comma-separated values between square brackets. See the following code.

# app.py

listA = ['Game Of Thrones', 8, 'Stranger Things', 3, 'Friends', 10]
listB = ['Emilia Clarke', 'Millie Bobby Brown', 'Jennifer Aniston']
print(listA)
print(listB)

See the following output.

➜  pyt python3 app.py
['Game Of Thrones', 8, 'Stranger Things', 3, 'Friends', 10]
['Emilia Clarke', 'Millie Bobby Brown', 'Jennifer Aniston']
➜  pyt

Similar to the string indices, list indices start at 0, and lists can be sliced, concatenated, and so on.

How to Access values in Python List

To access elements in the Python list, refer to the index number.

See the following code.

# app.py

listA = ['Game Of Thrones', 8, 'Stranger Things', 3, 'Friends', 10]
listB = ['Emilia Clarke', 'Millie Bobby Brown', 'Jennifer Aniston']
print(listA[2])
print(listB[1])

If we want to access values in lists, use the square brackets for slicing along with the index or indices to obtain value available at that index.

# app.py

listA = ['Game Of Thrones', 8, 'Stranger Things', 3, 'Friends', 10]
listB = ['Emilia Clarke', 'Millie Bobby Brown', 'Jennifer Aniston']
print("listA[2]: ", listA[2])
print("listB[1:2]: ", listB[1:2])

See the following output.

➜  pyt python3 app.py
listA[2]:  Stranger Things
listB[1:2]:  ['Millie Bobby Brown']
➜  pyt

Updating Python Lists

To update the list in Python, you can use the list index number and update the new element at the index. If we want to change the value of a specific element, then we have to refer to the index number. See the following code.

# app.py

listB = ['Emilia Clarke', 'Millie Bobby Brown', 'Jennifer Aniston']
print('Before change', listB)
listB[2] = 'Emma Mackey'
print('After change', listB)

See the following output.

➜  pyt python3 app.py
Before change ['Emilia Clarke', 'Millie Bobby Brown', 'Jennifer Aniston']
After change ['Emilia Clarke', 'Millie Bobby Brown', 'Emma Mackey']

Loop Through a List

To iterate a list in Python, use the for loop. You can use the for loop if. If we want to iterate through a loop. See the following code.

# app.py

listB = ['Emilia Clarke', 'Millie Bobby Brown', 'Jennifer Aniston']
for item in listB:
    print(item)

See the output.

➜  pyt python3 app.py
Emilia Clarke
Millie Bobby Brown
Jennifer Aniston
➜  pyt

Check if Element Exists

To check if an element exists in Python, use if…in condition. We can check if the item exists inside the list or not using if condition. See the following code.

# app.py

listB = ['Emilia Clarke', 'Millie Bobby Brown', 'Jennifer Aniston']
if "Millie Bobby Brown" in listB:
    print('Millie is Stranger Things Star')

See the following output.

➜  pyt python3 app.py
Millie is Stranger Things Star
➜  pyt

Python List Length

To check Python List length, use the len() function. If we want to determine how many elements a list has, use the len() method.

# app.py

listB = ['Emilia Clarke', 'Millie Bobby Brown', 'Jennifer Aniston']
print(len(listB))
See the following output.
➜  pyt python3 app.py
3
➜  pyt

Here, we get the output three because the list contains the three items.

Adding elements in Python List

To add elements to the Python list, use the list append() method. We can add or append the new element in the list using the append() method. See the following code.

# app.py

listB = ['Emilia Clarke', 'Millie Bobby Brown', 'Jennifer Aniston']
listB.append('Emma Mackey')
print(listB)

See the following output.

➜  pyt python3 app.py
['Emilia Clarke', 'Millie Bobby Brown', 'Jennifer Aniston', 'Emma Mackey']
➜  pyt

If we want to insert an element at the specified index, we can use the insert() method. See the following syntax of the insert() method.

insert(index, item)

See the following code example.

listB = ['Emilia Clarke', 'Millie Bobby Brown', 'Jennifer Aniston']
listB.insert(2, 'Emma Mackey')
print(listB)

See the following output.

➜  pyt python3 app.py
['Emilia Clarke', 'Millie Bobby Brown', 'Emma Mackey', 'Jennifer Aniston']
➜  pyt

See, we have inserted an element at index 2. Remember, the index starts at 2.

Removing an element from Python List

To remove an element from the Python list, use the list pop() method. There are several methods to remove the item from Python List. First, we will see the Python List pop() method.

listB = ['Emilia Clarke', 'Millie Bobby Brown', 'Jennifer Aniston']
listB.pop()
print(listB)

It will remove the last item from the list. So, in our case, Jennifer Aniston will be removed. See the following output.

➜  pyt python3 app.py
['Emilia Clarke', 'Millie Bobby Brown']
➜  pyt

Now, let’s see the Python List remove() method.

The Python remove() method removes the specified item from the list. We need to pass the item name.

# app.py

listB = ['Emilia Clarke', 'Millie Bobby Brown', 'Jennifer Aniston']
listB.remove('Emilia Clarke')
print(listB)

See the output.

➜  pyt python3 app.py
['Millie Bobby Brown', 'Jennifer Aniston']
➜  pyt

There is also one keyword called del, which also removes the item from the list. See the following example.

listB = ['Emilia Clarke', 'Millie Bobby Brown', 'Jennifer Aniston']
del listB[0]
print(listB)

See the output.

➜  pyt python3 app.py
['Millie Bobby Brown', 'Jennifer Aniston']
➜  pyt

The clear() method empties the list. See the following code.

listB = ['Emilia Clarke', 'Millie Bobby Brown', 'Jennifer Aniston']
listB.clear()
print(listB)

See the output.

➜  pyt python3 app.py
[]
➜  pyt

List() Constructor

You can use the list() constructor to make a new list. See the following example.

# app.py

listB = list(('Emilia Clarke', 'Millie Bobby Brown', 'Jennifer Aniston'))
print(listB)

In the above code, we need to add double rounded brackets. See the following output.

➜  pyt python3 app.py
['Emilia Clarke', 'Millie Bobby Brown', 'Jennifer Aniston']
➜  pyt

So, using the list() constructor to Make a List.

Copy a List

You cannot copy a list by merely typing list2 = list1, because list2 will only be a reference to list1, and changes made in list1 will automatically also be made in list2.

There are ways to make a; one way is to use the built-in List method copy().

See the following copy() method code example.

listB = list(('Emilia Clarke', 'Millie Bobby Brown', 'Jennifer Aniston'))
listM = listB.copy()
print(listM)

See the output.

➜  pyt python3 app.py
['Emilia Clarke', 'Millie Bobby Brown', 'Jennifer Aniston']
➜  pyt

#List Methods

Python has a set of built-in methods that you can use on lists.

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

 

That’s it for this tutorial.

Leave A Reply

Please enter your comment!
Please enter your name here

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