

The default values for list are 0 (startIndex) and the end (endIndex) of the list. This refers to the items of a list starting at index startIndex and stopping just before index endIndex. Lists can be sliced like strings and other sequences. > color_list="Yellow" #Change the third color Modify an element by using the index of the element > color_list.insert(2, "White") #Insert an item at third position See the following statements: > color_list= > print(color_list) # Creates Error as the indices is out of range > print(color_list,color_list) # Print First and Last Elements > color_list= # The list have four elements If you give any index value which is out of range then interpreter creates an error message. Let create a list called color_list with four items.Ĭolor_list= As positive integers are used to index from the left end and negative integers are used to index from the right end, so every item of a list gives two alternatives indices. If an index has a positive value it counts from the beginning and similarly it counts backward if the index has a negative value. List indices work the same way as string indices, list indices start at 0. > color_list = color_list1 + color_list2 + color_list3 Use + operator to create a new list that is a concatenation of two lists and use * operator to repeat a list. Also works on dictionary and set.įollowing list contains all integer values: > my_list1 = # the list contains all integer valuesįollowing list contains all string: > my_list2 = # the list contains all stringįollowing list contains a string, an integer and a float values: > my_list3 = # the list contains a string, an integer andĪ list without any element is called an empty list. # Removes first occurrence of item or raises ValueError. # Removes and returns item at index or from the end.

# Inserts item at index and moves the rest to the right. # Returns index of first occurrence or raises ValueError. Product_of_elems = functools.reduce(lambda out, x: out * x, ) Sorted_by_both = sorted(, key=lambda el: (el, el))įlatter_list = list(_iterable()) Sorted_by_second = sorted(, key=lambda el: el) How can I get the index of an element contained in the list?.Find largest and the smallest items in a list.Reverse the elements of the list in place.Return the number of times 'x' appear in the list.Return the index in the list of the first item whose value is x.Remove the item at the given position in the list, and return it.Modify an element by using the index of the element.
