Python List tutorial

Next

In this tutorial, we are going to see about Python List.

Python List is one of the most used data structure in Python. Python List is a collection of items in ordered sequence.There are 6 sequences in python and List is one of them.

Python list begins with an opening square bracket and ends with a closing square bracket, [] and you can put multiple data types into a list.

Let’s learn list with a lot of examples.

Create a list

You can create a list simply using square brackets.

#Create list with prepopulated elements list1=[1,2,3,4] print("List 1:",list1) #Create an empty list and add elements to it. list2=[] list2.append(1) list2.append(2) list2.append(3) list2.append(4) print("List 2:",list2)

Output:

List 1: [1, 2, 3, 4]
List 2: [1, 2, 3, 4]

Access list elements

You can access list elements with square brackets.Let’s see with the help of example.

listOfFruits=['Apple','Orange','Banana','Pineapple'] print("First fruit:",listOfFruits[0]) print("Second fruit:",listOfFruits[1]) print("Third fruit:",listOfFruits[2]) print("Fourth fruit:",listOfFruits[3])

Output:

First fruit: Apple
Second fruit: Orange
Third fruit: Banana
Fourth fruit: Pineapple

Slicing list

You can use slicing as similar to String in the list.
Use following syntax for slicing.
list1[start:end:step]
All the variables start, end and step are optional.
Start: It denotes starting of the list
Start: It denotes ending of the list
step: It denotes steps for accessing element.
Let’s understand with the help of example.

listOfFruits=['Apple','Orange','Banana','Pineapple'] #Print first two elements of the list print("First two elements:",listOfFruits[:2]) #Print last two items of the list print("Last two elements:",listOfFruits[2:]) #Print middle elements of the list print("Middle two elements:",listOfFruits[1:3]) #Print elements in step of 2 print("Elements in step 2",listOfFruits[::2])

Output:

First two elements: [‘Apple’, ‘Orange’]
Last two elements: [‘Banana’, ‘Pineapple’]
Middle two elements: [‘Orange’, ‘Banana’]
Elements in step 2 [‘Apple’, ‘Banana’]

Please go through above examples and you will be able understand how slicing works.Let’s
You can provide negative indexes also to traverse list in reverse order. -1 is the last index in the list.
Let’s understand with the help of example.

listOfFruits=['Apple','Orange','Banana','Pineapple'] #Print last three elements of the list print("last three elements:",listOfFruits[-3:]) #Print list in reverse order print("Print list in reverse order:",listOfFruits[::-1])

Output:

last three elements: [‘Orange’, ‘Banana’, ‘Pineapple’]
Print list in reverse order: [‘Pineapple’, ‘Banana’, ‘Orange’, ‘Apple’]

Python list methods

Please find list of methods which you can use with Python Lists.

Method Description
append This method is used to add single item to the end of the list.
extend This method is used to add list of items to the end of the list
remove This method is used to remove element from the list.If element is not present then it will raise error
index This method is used to find index of given element
pop This method will remove and return passed index.If index is not passed then it will remove and return last element from the list.
count This method will count occurences of passed element in the list
clear This method is used to remove all elements of the list.
copy This method is used to shallow copy the list.
sort This method is used to sort the list in ascending or desending order
reverse This method is used to reverse the list.
insert This method is used to add element to the list at specified index.

Get List’s length

You can use Python’s len function to get length of the list.

listOfFruits=['Apple','Orange','Banana','Pineapple'] print('Length of listOfFruits is:',len(listOfFruits))

Output:

Length of listOfFruits is: 4

Changing Values in a List with Indexes

You can change value at any index using = operator.
Let’s understand with the help of simple example.

listOfFruits=['Apple','Orange','Banana','Pineapple'] print("listOfFruits before:",listOfFruits) listOfFruits[1]='Mango' print("listOfFruits after:",listOfFruits)

Output:

listOfFruits before: [‘Apple’, ‘Orange’, ‘Banana’, ‘Pineapple’]
listOfFruits after: [‘Apple’, ‘Mango’, ‘Banana’, ‘Pineapple’]

As you can see, value at index 1 of the list changed from ‘Orange’ to ‘Mango’.

List Concatenation and List Replication

You can use + operator to concatenate lists.Please note that it won’t impact original list.If you want to make changes to first list, you need to explicitly assign it.

Let’s understand with the help of simple example.

list1=['Apple','Orange'] list2=['Banana','Pineapple'] print('ListOfFruits:',list1+list2)

Output:

ListOfFruits: [‘Apple’, ‘Orange’, ‘Banana’, ‘Pineapple’]

You can replicate list with the help of * operator

list1=['Apple','Orange'] print("List 1:",list1*2)

Output:

List 1: [‘Apple’, ‘Orange’, ‘Apple’, ‘Orange’]

That’s all about Python List.

Next

Join Our News Letter – Stay Updated

Subscribe to Awesome Java Content.




Add Comment

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.
You can like our facebook page Java2blog