
The simple way is to convert the list to the dictionary using the list item as keys. But in case, if you want to remove the duplicate from a list, how can you do it? #8) Python Remove Duplicates from the ListĪs you know, a list can contain duplicates. In the above example, we have taken a list of numbers and using the sum function we have added all the numbers. Python provides an in-built function called sum() which sums up the numbers in the list. In the above example, you can see that we have the my_foods list which we have appended in a string variable named as my_foods_csv using the join function.įinally, we print my_foods_csv string. My favorite foods are: pizza,falafel,carrot cake


Print("my favorite foods are:",my_foods_csv) For Example, convert a list to a comma separated string to save in a file. Sometimes it’s useful when you have to convert a list to string. Python join list means concatenating a list of strings to form a string. To prove that we actually have two separate lists, we’ll add new food to each list and show that each list keeps track of the appropriate person’s favorite foods: When we print each list, we see that they both contain the same foods. Later, we make a copy of my_foods by asking for a slice of my_foods without specifying any indices and store the copy in friend_foods. Then we make a new list called friend_foods. Print("\nMy friend's favorite foods are:")įirst, we create a list of the foods we like called my_foods. This friend likes everything in our list so far, so we can create that list by copying ours. This, in turn, will tell Python to make a slice that starts at the first item and ends with the last item, by producing a copy of the entire list.įor Example, imagine we have a list of our favorite foods and we want to make a separate list of foods that a friend likes. In order to copy a list, you can make a slice that includes the complete original list by omitting the first index and the second index (). Now, let’s explore how copying a list works and also examine a situation where copying a list is useful. Value Error: ‘Vammy’ is not in the list #5) Python Copy ListĪt times, You may want to start with an existing list and make an entirely new list based on the first one. If you search for an element which is not present in the list, then You will get an error. If the same element is present more than once, then it returns the position of the first element.

Index method is used to find a given element in the list and return to its position. Hence in order to get back to the original sequence of the list apply the reverse() method again to the same list. Reverse() method reverses the sequence of the list permanently. The reverse() method is used to reverse the sequence of the list and not to arrange it in a sorted order like the sort() method.

In order to reverse the original order of a list, you can use the reverse() method. You can also print the list in a reverse order using the sorted function in the following manner: The sorted() function allows you to display your list in a particular order, without affecting the actual order of the list.Īs you can see from the output, the original order of the list remains intact. In order to maintain the original order of the list that is present in sorted order, you can use the sorted() function. If you want to change the order of the list temporarily, then you need to use sorted() function. One more important thing to remember here is that sort() method changes the order of the list permanently. Thus sort() method is used to arrange a list in either Ascending or Descending order.
#Python sort list how to#
Now let’s see, How to sort the list in a Descending Order. If you want to sort the elements in Descending order, then you can use the following syntax. If you want to sort the elements in Ascending order, then you can use the following syntax. The sort() method is used to sort the elements in a specific order i.e. Let’s explore each of them in detail with examples. Python Advanced List includes the following concepts.
