Hello friends in this article we will discussed the important topic of the python is List which is Ordered and mutable. We will discussed all methods and other attributes in this article. We just share code for each method you have to run that code and check out the output of all this methods and analysis this output.
List : Lists are used to store multiple items in a single variable.- Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage.
- List is a collection which is ordered and changeable.
- Allows duplicate members.
- Lists are created using square brackets:
Example: list1 = ["apple", "banana", "cherry"] print(list1)
Ordered: it means that the items have a defined order, and that order will not change. If you add new items to a list, the new items will be placed at the end of the list.
Changeable: The list is changeable, meaning that we can change, add, and remove items in a list after it has been created.
Allow Duplicates Since lists are indexed, lists can have items with the same value
list1 = ["apple", "banana", "cherry", "apple", "cherry"] print(list1)
list = ["apple", "banana", "cherry"] print(len(list))
list1 = ["apple", "banana", "cherry"] list2 = [1, 5, 7, 9, 3] list3 = [True, False, False]print(list1)print(list2)print(list3)
A list with strings, integers and boolean values: list1 = ["abc", 34, True, 40, "male"]
list1 = ["apple", "banana", "cherry"] print(type(list1))
list1 = list(("apple", "banana", "cherry")) # note the double round-brackets print(list1)
- Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage.
- List is a collection which is ordered and changeable.
- Allows duplicate members.
- Lists are created using square brackets:
Access Items
Print the second item of the list:
list1 = ["apple", "banana", "cherry"]
print(list1[1])
Negative Indexing
Negative indexing means start from the end -1 refers to the last item, -2 refers to the second last item etc.
list1 = ["apple", "banana", "cherry"]
print(list1[-1])
Return the third, fourth, and fifth item:
list1 = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"] print(list1[2:5])
0 Comments