1. Lists in Python can contain elements of different data types. 2. Lists are written with square brackets and comma-separated elements. 3. List elements can be accessed by their integer index. 4. List elements can be updated, deleted, inserted, or appended using list methods like insert(), remove(), pop(), and append(). 5. Common list operations include sorting, reversing, copying, and checking membership.
1. Lists in Python can contain elements of different data types. 2. Lists are written with square brackets and comma-separated elements. 3. List elements can be accessed by their integer index. 4. List elements can be updated, deleted, inserted, or appended using list methods like insert(), remove(), pop(), and append(). 5. Common list operations include sorting, reversing, copying, and checking membership.
1. Lists in Python can contain elements of different data types. 2. Lists are written with square brackets and comma-separated elements. 3. List elements can be accessed by their integer index. 4. List elements can be updated, deleted, inserted, or appended using list methods like insert(), remove(), pop(), and append(). 5. Common list operations include sorting, reversing, copying, and checking membership.
1. Lists in Python can contain elements of different data types. 2. Lists are written with square brackets and comma-separated elements. 3. List elements can be accessed by their integer index. 4. List elements can be updated, deleted, inserted, or appended using list methods like insert(), remove(), pop(), and append(). 5. Common list operations include sorting, reversing, copying, and checking membership.
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)
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.