AppDividend
Latest Code Tutorials

Python List Example | List In Python Tutorial Explained

0

Python List Example | List In Python Tutorial Explained is today’s topic. There are four collection data types in the Python programming language:

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

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

Python List

The list is a most versatile datatype available in Python which can be written as a list of comma-separated values (items) between square brackets. An important thing about a list is that items in a list need not be of the same type.

A list may contain duplicate values with their distinct positions, and hence, multiple distinct or duplicate values can be passed as a sequence at the time of list creation.

Let’s creating a list 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.

#Accessing Values in Python Lists

You access the list items by referring 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])

See the following output.

➜  pyt python3 app.py
Stranger Things
Millie Bobby Brown
➜  pyt

In the above example, we have accessed the list items using their indices.

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

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

If we want to iterate through a list, then you can use the for 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

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

We can check the Python List length using the len() function. If we want to determine how many items 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.

#How To Add Items In Python List

We can use the Python List append() method to add the items in Python. We can add or append the new item 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 the index 2. Remember, the index starts at 2.

#Remove Item from Python List

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

#The 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 copy, and 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.

 

Finally, Python List Example | List In Python Tutorial Explained is over.

Leave A Reply

Your email address will not be published.

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