

We use the len() function to find the size of a list. 'Python' is present in languages, 'Python' in languages evaluates to True.'C' is not present in languages, 'C' in languages evaluates to False.We use the in keyword to check if an item exists in the list or not. We can use a for loop to iterate over the elements of a list. Sort the list in ascending/descending order Returns the count of the specified item in the list Returns the index of the first matched item Returns and removes item present at the given index Python has many useful list methods that makes it really easy to work with lists.Īdd all the items of an iterable to the end of the list Here, languages.remove('Python') removes 'Python' from the languages list. We can also use the remove() method to delete a list item. For example, languages = ĭel languages # In Python we can use the del statement to remove one or more items from a list. We then changed the value to 'C' using languages = 'C' Here, initially the value at index 3 is 'C++'. And we can change items of a list by assigning new values using the = operator. # insert an element at index 1 (second position) We use the insert() method to add an element at the specified index. Here, numbers.extend(even_numbers) adds all the elements of even_numbers to the numbers list. # add elements of even_numbers to the numbers list We use the extend() method to add all the items of an iterable (list, tuple, string, dictionary, etc.) to the end of the list. Here, append() adds 32 at the end of the array. In the above example, we have created a list named numbers. The append() method adds an item at the end of the list. Python list provides different methods to add items to a list. Meaning we can add and remove elements from a list. Note: When we slice lists, the start index is inclusive, but the end index is exclusive. my_list returns a list with items from index 5 to the end.my_list returns a list with items from index 2 to index 4.In Python, it is possible to access a portion of a list using the slicing operator. Note: If the specified index does not exist in a list, Python throws the Inde圎rror exception.

Print(languages) # Python Python Negative Indexing The index of -1 refers to the last item, -2 to the second last item and so on. Python allows negative indexing for its sequences. Hence, the first element of a list is present at index 0, not 1. Remember: The list index always starts with 0. And we have used the index number to access the items.

Here, we can see each list item is associated with the index number.

In the above example, we have created a list named languages. The index of the first element is 0, second element is 1 and so on. In Python, lists are ordered and each item in a list is associated with a number. Note: We can also create a list using the list() constructor. # list with elements of different data types store elements of different types (integer, float, string, etc.).Here, we have created a list named ages with 3 integer items. We create a list by placing elements inside, separated by commas.
