List in Python

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Python does not have built-in array types like Java, C or

C++. But it has a list that acts as an array.

What is a list?

Though there are six built-in data sequences in Python,


List, and tuples are the most used of all. The list is
written in comma-separated values (items) between
square brackets. Unlike an array, the data type of a list
does not have to be of the same type.

Below are some of the examples of the list which you


can see is comma separated. The data types are not the
same but of varied types.
cars = ['Maruti Suzuki', 'Fiat', 2019, 1000];
roll_number = [34, 2, 56, 45, 50 ];
alphabets = ["a", "b", "c", "d"];
As mentioned above, the index of the list starts from 0.
The list element can be accessed through its index

How to access list value

The value of the list can be accessed using its index.


Just write the list name and pass the index inside the
square bracket and its value can be displayed.
cars = ['Maruti Suzuki', 'Fiat', 2019, 1000];
roll_number = [34, 2, 56, 45, 50 ];
print ("cars in 0 index: ", cars[0])
print ("roll_numbe: ", roll_numbe[1:4])

After executing the code, there will be the


following results:
cars[0]: Maruti Suzuki
roll_number[1:4]: [34, 2, 56, 45]

Updating Lists

The list element can be updated also. This can be


implemented either through the slice or append ()
method.
Example :
cars = ['Maruti Suzuki', 'Fiat', 2019, 1000];
print ("At index 2 : ", cars[2])

Now, let’s update it


cars[2] = 2020
print ("New value at index 2 : ", cars[2])
Now, run the code in the terminal and you will get the
following results:
At index 2 : 2019
New value at index 2: 2019
Delete List Elements

Using the del statement we can delete the element of


any list. For that, you need to pass the index of the
element.
Example
cars = ['Maruti Suzuki', 'Fiat', 2019, 1000]
print (cars)
del cars[2]
print ("Now the new value at index 2 is: ", cars)
After executing the above code, you will get the
following results
['Maruti Suzuki', 'Fiat', 1000]

Check if the Item Exists

cars = ['Maruti Suzuki', 'Fiat', 2019, 1000]


if "Fiat" in cars:
print("Fiat is found inside cars")

Find the Length of the list

cars = ['Maruti Suzuki', 'Fiat', 2019, 1000]


print(len(cars))
Result
4
Add a new item to the list

cars = ['Maruti Suzuki', 'Fiat', 2019, 1000]


cars.append("Mercedez")
print(cars)
To add an item to the selected index, use the
insert() method:
Example
Insert an item as the second position:
cars = ['Maruti Suzuki', 'Fiat', 2019, 1000]
cars.insert(1, "Suvi 500")
print(cars)

Remove an element

The remove() method removes the specified item:


cars = ['Maruti Suzuki', 'Fiat', 2019, 1000]
cars.remove("Suvi 500")
print(cars)

Pop method

The pop() method removes the specified index.


If you don't specify the index, it will delete the last
element
cars = ['Maruti Suzuki', 'Fiat', 2019, 1000]
cars.pop()
print(cars)

The clear() method

As its name suggests, the clear method clears the


cars= ['Maruti Suzuki', 'Fiat', 2019, 1000]
cars.clear()
print(cars)

Copy a List

Make a copy of a list with the copy() method:


cars= ['Maruti Suzuki', 'Fiat', 2019, 1000]
newCar = cars.copy()
print(newCar)

The list() Constructor

We can also create a new list using the list constructor


colors= ['Red', 'Green', 4, 'L']
colors = list(("apple", "banana", "cherry"))
print(colors)
Extend Method:

Add the elements of a list, to the end of the current list


sizes = ['S', 'M', 'L']
colors = ['Red', 'Green', 'Blue']
sizes.extend(colors)

Reverses the order of the list

Reverse the order of the fruit list:


colors = ['Red', 'Green', 'Blue']
colors.reverse()

The sort method sorts the elements in


ascending order

colors = ['Red', 'Green', 'Blue']


colors.sort()

—-----------------------------------
Exercise:

1. Write a program to add 5 subjects to the List and


then show their names using the For loop.
2. Enter 10 random numbers to create a list and then
sort how many are odd and even, and show all odd
numbers and all even.
3. Create a list of 10 numbers and remove the last
element
a. Add two more elements
b. Remoe 8th element
c. Insert new two elements after 6th element
d. Find if 3 is inside the list
e. Show the list in Ascending order
f. Show in descending order
4. Write a Python program to sum all the items in a list.
5. Write a Python program to get the largest number from a list.
6. Write a Python program to get the smallest number from a list
7. Write a Python program to remove duplicates from a list.
8. Write a Python program to check if a list is empty or not.

You might also like