![]() ![]() The outputs of the above programs will be: Sorting a list my_list = copy_list = list(my_list) print(copy_list)Ī copy of the list my_list is created and printed. my_list = copy_list = my_py() print(copy_list)Īnother way to make a copy is to use the built-in method list(). There are ways to make a copy of a list, one way is to use the built-in list method copy(). ![]() (iv) clear() method - empties a list my_list = my_list.clear() print(my_list)Īll the elements are removed and the list becomes an empty list. The del keyword can also delete the list completely: my_list = del my_list print(my_list) #this will cause an error because we have deleted “my_list”. (iii) del keyword - removes the element at the specified index my_list = del my_list print(my_list) The last element ‘milk’ is removed from the list and the output becomes: For example, my_list = print(my_list.pop()) If the index is not provided, the pop() method removes the last element in the list. (ii) pop() method - removes an element at the given index The specified element ‘chocolate’ gets removed from the list and the output becomes: (i) remove() method - removes a specified element from the list my_list = my_list.remove(‘chocolate’) print(my_list) There are several methods to remove items from a list: my_list = print(“Number of elements in my list: ”, len(my_list))Īnd, the output gives: Number of elements in my list: 4 Removing elements from a list We can use the len() function to determine the length of a list, that is, the number of elements present in a list. Output: Yes, pizza is present in my list. See the example below, my_list = if ‘pizza’ in my_list: #checks if 'pizza' is present in the list print(“Yes, pizza is present in my list.”) else: print(“No, pizza is not present in my list.”) We can test if an element exists in a list or not, using the keyword ‘in’. The output will be: I like pizza I like sandwich I like salad Checking if an element exists in a list For example, my_list = for x in my_list: print(“I like ”, x) We can loop through the elements of a list using a for loop. ![]() You will find this function most useful in a competitive coding environment. Output: Enter the elements: 10 20 30 40 List is. Getting a list of numbers as input from user using the map() function: x = list(map(int,input(“Enter the elements“).strip().split(‘ ‘))) # Reads number inputs from user using map() function print("List is - “, x) The Python map() function is used to return a list of results after applying a given function to each item of an iterable(list, tuple etc). The output displays the string without those extra spaces at the beginning and at the end: programming Python map() Function The strip() method removes any leading (at the beginning) and trailing (at the end) characters in a string (by default, space is the character).įor example, text = “ programming “ my_text = text.strip() #Removes the spaces at the beginning and at the end of the string. The above program produces the following output: Īnother example to split a string into list with separator ‘-’: my_string = ‘abcd-efgh-ijkl’ my_list = my_string.split(‘-’) #split the text wherever there’s a hyphen(‘-') print(my_list) See the below example, which breakdowns a string of text into individual words: my_text = ‘I am learning Python!’ words = my_text.split() #splits the text wherever there is a space. If no separator is specified, then Python will use the white space in the string as the separation point. The separator tells Python where to break the string. ![]() split(separator) function to divide a string into a list of strings using a specified separator. Python List - II Splitting a string into a list in Python ![]()
0 Comments
Leave a Reply. |