Python List Tutorial


What is a list in Python?

List is a collection of items stored in a particular order. A python list can be of the letters of the alphabet, the digits from 0–9, or the names of all the people in your family. Also, we can store anything we want to store, irrespective of any correlation between them we can store anything lists.
In Python, square brackets ([]) indicate a list and individual elements in the list are separated by commas.

Here is an example of a list in Python Programming

Social_Media = ['youtube', 'instagram', 'twitter', 'snapchat']

print(Social_Media)
The print() function will print(represent) all elements of list.But what should we do to access each element separately? Let’s learn how to access elements of a list in python.

Accessing Elements in a Python List -

In the list, Items are ordered and in order to access any element we have to tell the Python interpreter the position, or index of the desired item. To access an element in a list, write the name of the list followed by the index of the item enclosed in square brackets.

For example

print(Social_Media[0])

The above command will print the first element of the Social_Media List.

Index Position in Python Start from 0, Not 1

Python considers the first item in a list to be at position 0, not position 1. This is true of most programming languages. The second item in a list has an index of 1. Using this simple counting system, you can get any element you want from a list by subtracting one from its position on the list. For instance, to access the fourth item in a list, you request the item at index 3.

Using Individual Values from a List

Python Lists allow us to use each element of the list separately as well as collectively. We can concat individual values with a message by using a variable and adding that particular element to that message.

For Example –

Social_Media = ['youtube', 'instagram', 'twitter', 'snapchat']

message = "My Social Media Account was on " + Social_Media [0].title() + "."

print(message)


In the above example, a sentence is built by using the 1st element of the list.
Output  – My Social Media Account was on Youtube.

Using this we can find the sum of elements in list python –

Sum =0

i=0

List = [4,6,8,2,6,7]

For i in [4,6,8,2,6,7]

       Sum = Sum + List

print(Sum)

Changing, adding, and removing elements –

In this section, we will learn about –

 1)How to add an element in list python programming?

2) How to remove an element from list python?

Lists that we create are of dynamic type mostly. Suppose we have to create a list of employees, so whenever a new employee is appointed the then we have to add a new item to the list, and also when an employee resigns or transferred we have to delete the information from the list.

 

Modifying elements in a Python List –

The syntax for modifying an element is similar to that of accessing an element. Whenever you want to modify an element then the most simple and significant way is to assign a new value to that index.

For example –

 
Social_Media = ['youtube', 'instagram', 'twitter', 'snapchat']

Social_Media[0] = “facebbok”

print(Social_Media)



The output will be –

= [' facebbok', 'instagram', 'twitter', 'snapchat']

Adding an element to a list in Python Programming –

In order to add an element in a list the method which is used to do so is insert(). We can do this by specifying the index of the new element and the value of the new item.

Example -      

motorcycles = ['honda', 'yamaha', 'suzuki']

motorcycles.insert(0, 'ducati')

print(motorcycles)


The above code will insert an element to the first place of the list.

Adding an element to the end of a List using Append in Python Programming –

The simplest way to add a new element to a list is to append the item to the list. When you append an item to a list, the new element is added to the end of the list.

motorcycles = ['honda', 'yamaha', 'suzuki']

print(motorcycles)

motorcycles.append('ducati')

print(motorcycles)

The above code will append a new element at the end of the python list.

Removing an element from a list python –

In this section, we will answer “how to remove an element from a list in python”. If we know the position of the item which we want to remove from a list, then simply we can use the del statement.

motorcycles = ['honda', 'yamaha', 'suzuki']

print(motorcycles)

del motorcycles[0]

print(motorcycles)


The above code will remove the 1st (index-0) element of list. In this way removing an element from a list python is done.

How to remove element from list python sing Pop() method?

The pop() method removes and returns the last item if the index is not provided. The term pop comes into our mind from stack, whenever pop is called it removes an element from the top of the stack. In this analogy, the top of a stack corresponds to the end of a list.

motorcycles = ['honda', 'yamaha', 'suzuki']

last_owned = motorcycles.pop()

print("The last motorcycle I owned was a " + last_owned.title() + ".")


Output –

The last motorcycle I owned was a Suzuki.

How to reverse a list in python?

In order to reverse a list in python, the method which is used to do so is reverse().The main thing to notice in this method is that this method doesn’t allow you to reverse the list alphabetically.

cars = ['bmw', 'audi', 'toyota', 'subaru']

 print(cars)

cars.reverse() print(cars)

OUTPUT –

['bmw', 'audi', 'toyota', 'subaru']

['subaru', 'toyota', 'audi', 'bmw']

Now in this way reversing a list is done in python programming.

How to find the length of a list in python?

To find the length of a list in python, we use len() method to find the length of a list in pythom programming.

cars = ['bmw', 'audi', 'toyota', 'subaru']

 len(cars)

Output – 4 This is termed as the length of a list in python programming.

List Comprehensions in Python (A Simplified way of creating List)

A list comprehension allows you to generate this same list in just one line of code. A list comprehension combines the for loop and the creation of new elements into one line and automatically appends each new element. List comprehensions are not always introduced to beginners, but we will discuss it in this section.

Example –

squares = [value**2 for value in range(1,11)]

 print(squares)


The above code will create a list of 10 elements, and each element will be the square of its position

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

A-List Comprehension can also contain Conditional Statements for creating a specific type of list.

For example –

even = [x for x in range(10)  if x % 2 == 0]
print(even)
Output -
[2,4,6,8,10]

 

 

 

 

 

 

 



Post a Comment

Previous Post Next Post