How to use List in Python - Coding Center

728x90 AdSpace

Followers

Trending
Friday, January 13, 2023

How to use List in Python



How to use List in Python

 

 

What is a list ?

List are used to store multiple items in a single variable.

 

Lists are one of 4 built-in data types in python used store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage.

 

Lists are created using square brackets:

 

Example 1:

 

Create a List by following code below:

 

thislist = ["apple", "banana", "cherry"]
print(thislist)

 

Then run in console using command python3 example1.py .

Look at the console, you have list is printed. Look at below:

 

['apple', 'banana', 'cherry']

 

 

List Items

 

List items are ordered, changeable, and allow duplicate values.

 

List Items are indexed, the first item has index  [0] , the second item has index [ 1 ] etc.

 

Ordered

 

When we say that list are ordered, it means that 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, remove items in a list after it has been created.

 

Allow Duplicates

 

since lists are indexed, list can have items with the same value:

 

Example 2:

 

look at the code below:

 

 

 

thislist = ["apple""banana""cherry""apple""cherry"]
print(thislist)

 

Then print example 2 using command python3 example2.py . You have duplicate list in console.

 

['apple', 'banana', 'cherry', 'apple', 'cherry']

 

 

List Length

 

to determine how many items a list has use the len() function:

 

Example 3 :

 

Print the number of items in the list by following code :

 

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

 

Then you will get the results as below:

 

3

 

You get the number three in the python console.

 

 

List Items Data Types

 

List items can be of any data type. See the example below:

We’ll create a list with items containing string, integer, and boolean

 

Example 4 :

 

string = ["apple", "banana", "cherry"]
integer = [1, 5, 7, 9, 3]
boolean = [True, False, False]

 

print(string)

print(integer)

print(boolean)

 

To get the result of the code above, then run using command python3 example4.py then enter. Then you will get the result as below.

 

['apple', 'banana', 'cherry']

[1, 5, 7, 9, 3]

[True, False, False]

 

List can also containing different data types. See the example below:

 

 

Example 5 :

 

list1 = ["abc", 34, True, 40, "male"]

print(list1)

 

Then you have result :

 

['abc', 34, True, 40, 'male']

 

 

Python Collections (Arrays)

 

There are four collection data types in the python programming language:

 

List is a collection which is ordered and changeable. Allow duplicate members.

Tuple is a collection which is ordered an unchangeble. Allow duplicate members.

Set is a collection which is unordered, unchangable, and unindexed. No duplicate members.

Dictionary is a collection which is ordered and changeable. No duplicate members.

 

 

Access List Items

 

List items are indexed and you can access them by referring to index number. An example of accessing the list can be seen in the code below.

 

Print the secon item of the list and give the file name access_list.py :

 

Example 5 :

 

thislist = ["apple""banana""cherry"]
print(thislist[1])

 

Then run in your console by using command python3. You will get the result as below:

 

banana

 

It can be seen that the console screen displays “banana”.

 

Note : The first item has index 0

 

 

 

Negative Indexing

 

Negative indexing means start from the end. -1 refers to the last item, -2 refers to the second last item etc.

 

Example Access List Items By Negative Indexing :

 

Print the last item of the list:

 

Example 6 :

 

thislist = ["apple""banana""cherry"]
print(thislist[-1])

 

The result will print “cherry” on the console.

 

 

Range of Indexes

 

You can specify a range of indexes by speciying to start and where to end the range. When specifiying a range, the return value will be a new list with the specified items.

 

We have example to return the third, fourth, and fifth item by following code below:

 

Example 7 :

 

thislist = ["apple""banana""cherry""orange""kiwi""melon""mango"]
print(thislist[2:5])

 

The result can be seen as below:

 

['cherry', 'orange', 'kiwi']

 

Note: The search will start at index 2 (included) and end at index 5 (not included).

 

Remember that the first item has index 0.

 

By leaving out the start value, the range will start at the first item.

 

Example 8 :

 

This example returns the items from the beginning to, but NOT including “kiwi”

 

thislist = ["apple""banana""cherry""orange""kiwi""melon""mango"]
print(thislist[:4])

#This will return the items from index 0 to index 4.

 

#Remember that index 0 is the first item, and index 4 is the fifth item

#Remember that the item in index 4 is NOT included

 

 

 

The result can be seen as below:

 

['apple', 'banana', 'cherry', 'orange']

 

 

By leaving out the end value, the range will go on the end of the list.

 

Example 9 :

 

this example returns the items from “cherry” to the end:

 

thislist = ["apple""banana""cherry""orange""kiwi""melon""mango"]
print(thislist[2:])

 

The result can be seen as below:

 

['cherry', 'orange', 'kiwi', 'melon', 'mango']

 

 

 

Range of Negative Indexes

 

Specify negative indexes if you want to start the search from the end of the list.

 

Example 10:

 

This example returns the items from “orange” (-4) to, but NOT including “mango” (-1):

 

thislist = ["apple""banana""cherry""orange""kiwi""melon""mango"]
print(thislist[-4:-1])

 

The result can be seen as below:

 

['orange', 'kiwi', 'melon']

 

 

 

Check if Item Exists

 

To determine if a specified item is present in a list use the in keyword.

 

 

 

 

Example 11 :

 

thislist = ["apple""banana""cherry"]
if "apple" in thislist:
  print("Yes, 'apple' is in the fruits list")

 

 

The result can be seen as below:

 

Yes, 'apple' is in the fruits list

 

 

 

 

 

How to use List in Python Reviewed by Zidane on January 13, 2023 Rating: 5 How to use List in Python     What is a list ? List are used to store multiple items in a single variable.   Lists are one of 4 built-in dat...

No comments: