Python List | Python Collections (Arrays) | List | Loop Through an inventory | Check if Item Exists | List Length | Add Items | Remove Item | Copy an inventory | The list() creator


Python List | Python Collections (Arrays) | List | Loop Through an inventory | Check if Item Exists | List Length | Add Items | Remove Item | Copy an inventory | The list() creator

Python Collections (Arrays)

There area unit four assortment information varieties within the Python programming language:

List may be a assortment that is ordered and changeable. permits duplicate members.
Tuple may be a assortment that is ordered and stablepermits duplicate members.
Set may be a assortment that is unordered and unindexed. No duplicate members.
Dictionary may be a assortment that is unordered, changeable and indexed. No duplicate members.
When selecting a group sortit’s helpful to know the properties of that sortselecting the proper sort for a specificinformation set might mean retention of that means, and, it might mean a rise in potency or security.

List

A list may be a assortment that is ordered and changeable. In Python lists area unit written with sq. brackets.

Example
Create a List:

thislist = [“apple”, “banana”, “cherry”]
print(thislist)

Access things

You access the list things by bearing on the index number:

Example
Print the second item of the list:

thislist = [“apple”, “banana”, “cherry”]
print(thislist[1])

Change Item price
To change the worth of a selected item, talk to the index number:

Example
Change the second item:

thislist = [“apple”, “banana”, “cherry”]
thislist[1] = “blackcurrant”
print(thislist)


Loop Through an inventory

You can loop through the list things by employing a for loop:

Example
Print all things within the list, one by one:

thislist = [“apple”, “banana”, “cherry”]
for x in thislist:
print(x)

You will learn a lot of concerning for loops in out Python For Loops Chapter.

Check if Item Exists

To determine if a mere item is gift in a very list use the in keyword:

Example
Check if “apple” is gift within the list:

thislist = [“apple”, “banana”, “cherry”]
if “apple” in thislist:
print(“Yes, ‘apple’ is within the fruits list”)

List Length

To determine what percentage things an inventory has, use the len() method:

Example
Print the amount of things within the list:

thislist = [“apple”, “banana”, “cherry”]
print(len(thislist))

Add Items

To add associate degree item to the tip of the list, use the append() method:

Example
Using the append() technique to append associate degree item:

thislist = [“apple”, “banana”, “cherry”]
thislist.append(“orange”)
print(thislist)

To add associate degree item at the required index, use the insert() method:

Example
Insert associate degree item because the second position:

thislist = [“apple”, “banana”, “cherry”]
thislist.insert(1, “orange”)
print(thislist)

Remove Item

There area unit many ways to get rid of things from a list:

Example
The remove() technique removes the required item:

thislist = [“apple”, “banana”, “cherry”]
thislist.remove(“banana”)
print(thislist)

Example
The pop() technique removes the required index, (or the last item if index isn’t specified):

thislist = [“apple”, “banana”, “cherry”]
thislist.pop()
print(thislist)

Example
The del keyword removes the required index:

thislist = [“apple”, “banana”, “cherry”]
del thislist[0]
print(thislist)

Example
The del keyword can even delete the list completely:

thislist = [“apple”, “banana”, “cherry”]
del thislist

Example

The clear() technique empties the list:


thislist = [“apple”, “banana”, “cherry”]
thislist.clear()
print(thislist)

Copy an inventory

You cannot copy an inventory just by typewriting list2 = list1, because: list2 can solely be a relevance list1, and changes created in list1 can mechanically even be created in list2.

There area unit ways that to form a duplicate, {one way|a technique|a technique|a way} is to use the intrinsic List method copy().

Example
Make a duplicate of an inventory with the copy() method:

thislist = [“apple”, “banana”, “cherry”]
mylist = thislist.copy()
print(mylist)

Another way to form a duplicate is to use the intrinsic technique list().

Example
Make a duplicate of an inventory with the list() method:

thislist = [“apple”, “banana”, “cherry”]
mylist = list(thislist)
print(mylist)

The list() creator

It is additionally potential to use the list() creator to form a replacement list.

Python List | Python Collections (Arrays) | List | Loop Through an inventory | Check if Item Exists | List Length | Add Items | Remove Item | Copy an inventory | The list() creator

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories