Javaexercise.com

Python List

A list in Python is a data structure that is used to store data. It is a collection of comma-separated values enclosed within a square bracket ([]).

Python list can contain any type of data but mostly is used to collect a similar type of values.

The list is an index-based data structure. It starts from 0 and goes to n-1 where n is length of the list. The first element of list always will be at 0 index and the last will be at n-1 index.

In this topic, we will learn to create lists, access list elements, update list, remove list elements, merge two lists into a single list, traversing list elements , etc.

Advantages of Python List

  • List can be indexed and sliced
  • It is mutable
  • It is ordered (store elements in insertion order)
  • Provides built-in methods

How to create a list in Python?

Let's start with an example to create a list. Here, we are creating several lists: list of string values, list of numerical values, list of characters, and a list of mixed type values. This example shows that we can create a list of any type of values.

# Example to demonstrate python list

# list of string
fruits = ["Mango", "Orange", "Apple"]
print(fruits)

# list of numbers
numbers = [100,25,152,365,845]
print(numbers)

# list of characters
vowels = ['a','e','i','o','u']
print(vowels)

# list of mix values
mix = ["India", 12, 'A', 10.50]
print(mix)

Output:

['Mango', 'Orange', 'Apple']
[100, 25, 152, 365, 845]
['a', 'e', 'i', 'o', 'u']
['India', 12, 'A', 10.5]

How to access elements from a list in Python?

After creating a list, now let's access its elements. To access list elements, we can use index value. Since the list is an index-based data structure so by using the index value we can access any value of the list.

Note: While passing index value, make sure it is in range else compiler throws an error: IndexError: list index out of range.

# Example to demonstrate access python list elements

# list of integers
integers = [100,25,152,365,845]
print(integers)

# Access first element
item = integers[0] # index value starts with 0
print(item)

# Access Third element
item = integers[2]
print(item)

# Access Last element
item = integers[len(integers)-1]
print(item)

Output:

[100, 25, 152, 365, 845]
100
152
845

Negative Indexing (Reverse Order Accessing) in Python List

If we want to access list elements in reverse order, means access elements from end to start then we can use negative index. The negative index works similarly indexing as we did in the above example except that it access elements from the end of the list. The last element of a list always present at -1 index. Since negative indexing starts from -1. See the example here.

# Example to demonstrate access python list elements

# list of integers
integers = [100,25,152,365,845]
print(integers)

# Access last element
item = integers[-1] # last index value starts with -1
print(item)

# Access third from last element
item = integers[-3]
print(item)

Output:

[100, 25, 152, 365, 845]
845
152

How to slice lists in Python?

Slicing is a technique of fetching elements by specifying the start and end index. We can use slicing to fetch a sub-list or some part or a whole list as a list. Although the start and end index both are optional, we can use them if required. See the example here.

# Example to demonstrate slicing in Python list

# list of integers
integers = [100,25,152,365,845]
print(integers)

# Access all elements
item = integers[:] # 0 to last
print(item)

# Access first three elements
item = integers[:3] # 0 to third
print(item)

# Access 1 to 3 elements
item = integers[1:4] 
print(item)

# Access all from 2nd element
item = integers[2:]
print(item)

Output:

[100, 25, 152, 365, 845]
[100, 25, 152, 365, 845]
[100, 25, 152]
[25, 152, 365]
[152, 365, 845]

How to change or update elements of the Python list?

To change or update list elements you can simply assign the new value to the desired index. Since the list is mutable so it is easy to update a modify list elements.

# Example to demonstrate Python list

# list of integers
integers = [100,25,152,365,845]
print(integers)

# change 2nd element of list
integers[1] = 50
print(integers)

# change last element of list
integers[len(integers)-1] = 800 # update last element
print(integers)

Output:

[100, 25, 152, 365, 845]
[100, 50, 152, 365, 845]
[100, 50, 152, 365, 800]

How to delete or remove elements from Python list?

To remove elements from a list there are several ways like remove() method that remove the specified elements or pop() method that removes the element from the specified index or simply use del keyword to remove the element. Here, in this example, we used various ways to remove the list element.

# Example to demonstrate Python list

# list of integers
integers = [100,25,152,365,845]
print(integers)

# Remove specified item
integers.remove(152)
print(integers)

# Remove specified item by index
integers.pop(0) # pop() method
print(integers)

# Remove specified item by index
del integers[0] # del keyword
print(integers)

Output:

[100, 25, 152, 365, 845]
[100, 25, 365, 845]
[25, 365, 845]
[365, 845]

How to Iterating Python list elements?

To traverse list elements, we can use for loop that is used to iterate over iterable. 

# Example to demonstrate Python list

# list of integers
integers = [100,25,152,365,845]
print(integers)

# Traversing list elements
for item in integers:
  print(item)
  

Output:

[100, 25, 152, 365, 845]
100
25
152
365
845

How to create Nested List in Python

A list inside another list is known as a nested list. Python allows us to create a nested list to collect a variety of data into a single list.

# Example to demonstrate python nested list

# list of integers
integers = [100,25,152,365,845]
print(integers)

# nested list
numbers = [10.5, 25.5, 23.40, integers]
print(numbers)

# nested list
numbers = [10.5, 25.5, 23.40, [100,25,152,365,845]]
print(numbers)

Output:

[100, 25, 152, 365, 845]
[10.5, 25.5, 23.4, [100, 25, 152, 365, 845]]
[10.5, 25.5, 23.4, [100, 25, 152, 365, 845]]

Combine Python Lists or Concatenate two Python lists

We can merge or combine two lists into a single list by using the plus (+) operator. It is used to concatenate two lists into a single list. See the example here.

# Example to demonstrate python list joining

# list of integers
integers = [100,25,152,365,845]
print(integers)

# list of floating values
float_numbers = [10.5, 25.5, 23.40]
print(float_numbers)

# Join Lists
numbers = integers+float_numbers
print(numbers)

Output:

[100, 25, 152, 365, 845]
[10.5, 25.5, 23.4]
[100, 25, 152, 365, 845, 10.5, 25.5, 23.4]

How to Search for elements in Python list?

We can use in or not in keywords to check whether a list contains the element or not. It simply returns true if the element is present in the list, false otherwise.

# Example to demonstrate Python list

# list of integers
integers = [100,25,152,365,845]
print(integers)

# Find 25 in list
val = 25 in integers
print(val)

# find 125 in list
val = 125 not in integers
print(val)

# find 100 in list
val = 100 not in integers
print(val)

Output:

[100, 25, 152, 365, 845]
True
True
False


Conclusion

Well, in this topic, we have learned to create a list, access, update, delete its elements. We created nested list, combine two lists into a single list, traverse list elements etc.

If we missed something, you can suggest us at - info.javaexercise@gmail.com