Lists in Python are like dynamic arrays. One list can contain different data types like strings, integers, and objects. The elements of a list are arranged with a definite sequence with indices starting from 0.

  1. Creating a list
  2. Accessing elements of a list
  3. Checking a certain element in a list
  4. Determine list length
  5. Adding an element to a list
  6. Removing an element from a list
  7. Copy a list
  8. Concatenate two lists

Python Lists

Creating a list

There are two ways of creating a list in Python. The first one is placing your elements with the desired sequence between square brackets [] and assigning it to a variable. Let’s see the syntax:

Code:

Fruit = ["Banana","Grapes","Orange" ]
print(Fruit)

Output:

['Banana', 'Grapes', 'Orange']

The second method is using a list() constructor and placing your elements inside it between brackets(). Let’s see the syntax:

Code:

Fruit = list(("Banana","Grapes","Orange"))
print(Fruit)

Output :

['Banana', 'Grapes', 'Orange']

Accessing elements of a list

Accessing elements of a list happens through choosing a specific index where the desired element is present. There are two types of indexing, positive indexing and a negative one.

Let’s start with the positive one. Where you write the name of the list followed by square brackets with index included in it. Let’s see the syntax:

Code:

Fruit = ["Banana","Grapes","Orange" ]
print(Fruit[1])

Output:

Grapes

Lists offer to access a range of elements of a list by choosing the starting and ending index, where the starting index is included and the ending index is not included. Let’s see the syntax:

Code:

Fruit = ["Banana","Grapes","Orange", "Mango", "Cherry", "Kiwi" ]
print(Fruit[1:4])

Output:

['Grapes', 'Orange', 'Mango']

Same as positive indexing, the negative indexing syntax is the same but the index is written as a negative number where -1 is the end of the list and the number decreases as you go to the start of the list. Let’s see the syntax:

Code:

Fruit = ["Banana","Grapes","Orange", "Mango", "Cherry", "Kiwi" ]
print(Fruit[-3])

Output:

Mango

Range of negative indices, same as the normal range of indices where the starting index is included and the last index is not included. Let’s see the syntax:

Code:

Fruit = ["Banana","Grapes","Orange", "Mango", "Cherry", "Kiwi" ]
print(Fruit[-5:-1])

Output:

['Grapes', 'Orange', 'Mango', 'Cherry']

Checking a certain element in a list

By using an “if” and “in” keywords you can check whether a certain element is in the list. Let’s see the syntax:

Code:

Fruit = ["Banana","Grapes","Orange", "Mango", "Cherry", "Kiwi" ]
if "Mango" in Fruit:
print("Mango is in the list")

Output:

Mango is in the list

Determine list length

To determine the size of a list or the number of elements within the list, Python provides the function len(). Let’s see the syntax:

Code:

Fruit = ["Banana","Grapes","Orange", "Mango", "Cherry", "Kiwi" ]
print(len(Fruit))

Output:

6

Adding an element to a list

You can simply add an element to the end of the list by using append() function. Let’s see the syntax:

Code:

Fruit = ["Banana","Grapes","Orange", "Mango", "Cherry", "Kiwi" ]
Fruit.append("Melon")
print(Fruit)

Output:

['Banana', 'Grapes', 'Orange', 'Mango', 'Cherry', 'Kiwi', 'Melon']

Also, you can add an element to a specific index by using insert() function where you pass the desired index and the new element to it. Let’s see the syntax:

Code:

Fruit = ["Banana","Grapes","Orange", "Mango", "Cherry", "Kiwi" ]
Fruit.insert(4,"Melon")
print(Fruit)

Output:

['Banana', 'Grapes', 'Orange', 'Mango', 'Melon', 'Cherry', 'Kiwi']

Removing an element from a list

There are several methods to remove an element from a list. The first method is the remove() function where you pass the value of the desired element to it. Let’s see the syntax:

Code:

Fruit = ["Banana","Grapes","Orange", "Mango", "Cherry", "Kiwi" ]
Fruit.remove("Orange")
print(Fruit)

Output:

['Banana', 'Grapes', 'Mango', 'Cherry', 'Kiwi']

The second method is the pop() function where you pass to it the desired index to be removed from the list but if no index were passed to it, it will automatically remove the last item in the list. Let’s see the syntax:

Code:

Fruit = ["Banana","Grapes","Orange", "Mango", "Cherry", "Kiwi" ]
Fruit.pop(2)
print(Fruit)

Output:

['Banana', 'Grapes', 'Mango', 'Cherry', 'Kiwi']

The third method is the del keyword which depends also on the index desired to be removed with different syntax than pop() function. Where the keyword is followed by the list name followed by the desired index between square brackets[]. Let’s see the syntax:

Code:

Fruit = ["Banana","Grapes","Orange", "Mango", "Cherry", "Kiwi" ]
del Fruit[2]
print(Fruit)

Output:

['Banana', 'Grapes', 'Mango', 'Cherry', 'Kiwi']

Copy a list

There are several methods to copy a list to another. The first method is to use the copy() function where you declare a new variable and assign to it the original list attached to it the copy() function. Let’s see the syntax:

Code:

Fruit = ["Banana","Grapes","Orange", "Mango", "Cherry", "Kiwi" ]
New_Fruits = Fruit.copy()
print(New_Fruits)

Output:

['Banana', 'Grapes', 'Orange', 'Mango', 'Cherry', 'Kiwi']

The second method is using the list() constructor by declaring a variable and assigning it the list() constructor, but in this case, you will pass the original list as a constructor parameter. Let’s see the syntax:

Code:

Fruit = ["Banana","Grapes","Orange", "Mango", "Cherry", "Kiwi" ]
New_Fruits = list(Fruit)
print(New_Fruits)

Output:

['Banana', 'Grapes', 'Orange', 'Mango', 'Cherry', 'Kiwi']

Concatenate two lists

There are several methods to concatenate or join lists in Python. The first method is simply declaring a new variable and assigning to it the summation of the two desired lists. Let’s see the syntax:

Code:

Fruit = ["Banana","Grapes","Orange", "Mango", "Cherry", "Kiwi" ]
Color = list(("Blue" ,"Green", "Yellow"))
Joined = Fruit + Color
print(Joined)

Output:

['Banana', 'Grapes', 'Orange', 'Mango', 'Cherry', 'Kiwi', 'Blue', 'Green', 'Yellow']

The second method is by manually appending all the elements from the second list to the first one using a for loop and append() function. Let’s see the syntax:

Code:

Fruit = ["Banana","Grapes","Orange", "Mango", "Cherry", "Kiwi" ]
Color = list(("Blue" ,"Green", "Yellow"))
for x in Color:
Fruit.append(x)
print(Fruit)

Output:

['Banana', 'Grapes', 'Orange', 'Mango', 'Cherry', 'Kiwi', 'Blue', 'Green', 'Yellow']

The third method is using the extend() function where you attach the first list name attached to it extend() function and pass to it the second list name. Let’s see the syntax:

Code:

Fruit = ["Banana","Grapes","Orange", "Mango", "Cherry", "Kiwi" ]
Color = list(("Blue" ,"Green", "Yellow"))
Fruit.extend(Color)
print(Fruit)

Output:

['Banana', 'Grapes', 'Orange', 'Mango', 'Cherry', 'Kiwi', 'Blue', 'Green', 'Yellow']