What is the code to print the element banana fruits apple, mango grapes papaya banana pomegranate

Use the correct method to add multiple items (more_fruits) to the fruits set.

fruits = {"apple", "banana", "cherry"} more_fruits = ["orange", "mango",

"grapes"] @(26)

fruits = {"apple", "banana", "cherry"} more_fruits = ["orange", "mango",

"grapes"] fruits.update(more_fruits)

Correct!

Next ❯

There are three ways in which you can add elements to a list.

Iterating a list is nothing but spanning each and every element of a list.
Generally we iterate a list using a for loop.

Scenario:
Given a following string:

1. Example Using Split():

3.Example using Find():

Find() function will display the index position of any element, delimiter or a string.

List comprehension is an elegant way to define and create list in Python. These lists have often the qualities of sets, but are not in all cases of sets.

A list in Python is a mutable and ordered sequence of elements. It is a built-in data structure and is one of several compound data types supported by Python. Each element that is contained in a list is called an item. A list can contain items of different data types which makes lists very powerful. Python lists can also contain duplicate items.

A Python list is created by specifying items as comma-separated values inside square brackets [ ].

Like strings, lists in Python can also be indexed. Each item of a list corresponds to an index number. The first item has an index 0.

We can use a positive index number in square brackets and access a list item as shown below:

Similar to strings, Python lists also support negative index numbers. We can use negative indices with lists to start counting from the end of the list, starting at the index number -1.

Since lists in Python are mutable, we can modify the items in a list. This is done using the assignment operator as shown below:

We can concatenate two or more lists together using the + operate as shown below:

The + operator can also be used to add an item or multiple items (in list form) to the end of a list as shown below:

We can also use the += compound operator with lists as shown below:

We can multiply lists using the * operate as shown below:

We can also use the *= compound operator with lists as shown below:

We can use the 'del' statement to remove an item from a list at a specified index number as shown below:

We can create a list which contains another list as an item. A list within another list is called a nested list.

Like strings, lists in Python can also be sliced. A slice can be created by using a range of index numbers separated by a colon in the format [x:y]. Here, the first index number is inclusive and specifies where the slice starts from. The second index number is exclusive and specifies where the slice ends.

We can include either end of a list by omitting one of the index numbers. For example, to create a slice that starts at the beginning of the list and ends in the middle, we have to specify only the index number after the colon as shown below:

Likewise, we can create a slice that starts in the middle of the list and includes each item until the end of the list by specifying only the index number before the colon as shown below:

We can also omit both the index numbers to create a slice that starts at the head of the list and ends at the tail of the list. This approach is helpful in combination with the stride parameter which is discussed below in this article.

It is also possible to use negative index numbers for list slicing.

In the examples above, we have used two index numbers to slice a list. It is also possible to use a third parameter called 'stride' which specifies how many items to move forward after the first item has been retrieved from the list. The default value of stride is 1.

A negative value for stride specifies that we want to extract items from the list in reverse order.

Lists support a number of methods to perform common operations. It is important to remember that lists are mutable in Python. Therefore, invoking any method on a list will affect the list itself and not a copy of the list. Some of the most commonly used Python list methods are listed below.

It adds a specified item to the end of the list.

It inserts an item at a specified index number. The first argument is the index number at which the item needs to be inserted and the second argument is the item itself.

It removes the first item from the list whose value is equal to the specified value.

Trying to remove an item which is not present in the list results in an error.

It removes the item at the specified index number in the list and returns it. If no index number is specified, it removes and returns the last item in the list.

It returns the index of the first item in the list whose value is equal to the specified value.

It produces an error if there is no item in the list with the specified value.

It returns the number of times the specified value exists in the list.

It is used to sort the items in a list.

It reverses the order of items in a list.

It removes all the items from a list.

It makes a copy of a list. It is helpful in situations when a list needs to be manipulated while still having the original list available to us unchanged.

It is used to combine lists and takes in a second list as its argument.