Python Lists with example


Python Lists with example

List in Python or Python Lists is a built-in data type of Python or collection data types of Python programming. It is used to store multiple values in a single variable. Also, we can store multiple values of different data types in a list variable such as integer, string, boolean, etc. It is declared by using the second bracket.

Also read, Variables in Python with Examples

Basically, Python Lists are of three types as given below

  • Ordered:- In the ordered list, items are arranged in a fixed order that can not be changed. If a new item is added, it will be placed at the end of the list values.
  • Changeable:- A list is changeable which means we can change the order of items in the list, add new items or remove items from the list variable.
  • Allow Duplicates:- We can add duplicate values in a list and the same values are accessed according to the index as the index starts from the position ‘0’.

Below are some examples of List

Example 1:-

carlist = ["Maruti","Volvo","BMW","Toyota"]
print(carlist)

Output:
['Maruti', 'Volvo', 'BMW', 'Toyota']

Example 2:-

mylist = ["xyz",23,"abc",10.5,False]
print(mylist)

Output:
['xyz', 23, 'abc', 10.5, False]

Example 3:-

mylist = [1,2,3,4,4]
print(mylist)

Output:
[1, 2, 3, 4, 4]

Length of the list:-

We can find the length of the list which means the total number of items or values in a list with the help of len() function as shown below

Example 4:-

mylist = [1,2,3,4,5]
print(len(mylist))

Output:-
5

List Constructor in Python

Also, we can store multiple values in a single variable using the list() constructor as shown below

Example 5:-

mylist = list(("maruti","volvo","BMW","Toyota"))
print(mylist) 

Output:
['maruti', 'volvo', 'BMW', 'Toyota']

Use the double-rounded bracket while using the list constructor.

Access Items in a List:- (Python)

We can access items in a list by using the index numbers from the list as you know the index starts from zero.

Example 6:-

mylist = ["maruti","volvo","BMW","Toyota"]
print(mylist[2])

Output:
BMW

Access Items in a List using negative index

We can also access any item in a list using the negative index numbers such as

  • ‘-1’ indicates the last item from the list
  • ‘-2’ indicates the second last item from the list
  • ‘-3’ indicates the third last item from the list

Example 7:-

mylist = ["maruti","volvo","BMW","Toyota"]
print(mylist[-3])

Output:
volvo

Access items in a list using range of index

We can also access the items in a list using the range of index just by putting the start index number and end index number as shown below

Example 8:-

mylist = ["maruti","volvo","BMW","Toyota"]
print(mylist[1:3])

Output:
['volvo', 'BMW']

How to check if an item exists in the list (Python)

Here we can determine whether an item exists in the list or not as shown below

Example 9:-

carlist = ["maruti","volvo","BMW","Toyota"]
if "BMW" in carlist:
   print("yes. BMW is in the list")

Output:
yes. BMW is on the list

Change item value in a list (Python)

Here, we can change the value of an item in the list by using an index number of the particular item as shown below

Example 10:-

carlist = ["maruti","volvo","BMW","Toyota"]
carlist[1] = "Scorpio"
print(carlist )

Output:
['maruti', 'Scorpio', 'BMW', 'Toyota']

Change a range of item values (Python)

Also, we can change a range of item values in a list using a range of index numbers separated by a colon (:) as shown below

Example 11:-

carlist = ["maruti","volvo","BMW","Toyota"]
carlist[1:3] = ["Scorpio","Bolero"]
print(carlist)

Output:
['maruti', 'Scorpio', 'Bolero', 'Toyota']

Append an item in a list in Python

If you want to append an item in a list at the end position then you can do so by using the append() function as shown below

Example 12:-

carlist = ["maruti","volvo","BMW","Toyota"]
carlist.append("Marcetis")
print(carlist)

Output:
['maruti', 'volvo', 'BMW', 'Toyota', 'Marcetis']

Insert an item in a list in Python at a particular index

If you want to insert an item at a particular index then you can do so by putting an index number inside the insert() function.

Example 13:-

carlist = ["maruti","volvo","BMW","Toyota"]
carlist.insert(1,"Marcetis")
print(carlist)

Output:
['maruti', 'Marcetis', 'volvo', 'BMW', 'Toyota']

Now, you can see that the index number of ‘volvo’ is 1 before, after inserting ‘marcetis’, it goes to index number ‘2’

Extend List in Python

If you want to merge items of two different lists into a single list then you can do so with the help of extend() function.

Example14:-

carlist = ["maruti","volvo","BMW","Toyota"]
bykelist = ["Honda","Bazaz","TVS"]
carlist.extend(bykelist)
print(carlist)

Output:
['maruti', 'volvo', 'BMW', 'Toyota', 'Honda', 'Bazaz', 'TVS']

Remove items from a list in Python

If you want to remove an item from the list then you can do so by the remove() function as shown below

Example 15:-

carlist = ["maruti","volvo","BMW","Toyota"]
carlist.remove("volvo")
print(carlist)

Output:
['maruti', 'BMW', 'Toyota']

Remove a specific item from a list in Python

If you want to remove a specific item from the list then you can do so by using the particular index number inside the pop() function as shown below

Example 16:-

carlist = ["maruti","volvo","BMW","Toyota"]
carlist.pop(2)
print(carlist)

Output:
['maruti', 'volvo', 'Toyota']

Without passing the index number inside the pop() function will remove the last item from the list by default.

Remove a specific item from the list using the del keyword

If you want to delete an item from the list using the del keyword then you can do so as shown below

Example 17:-

carlist = ["maruti","volvo","BMW","Toyota"]
del carlist[1]
print(carlist)

Output:
['maruti', 'BMW', 'Toyota']

Empty the items in a list in Python

Example 18:-

carlist = [“maruti”,”volvo”,”BMW”,”Toyota”]
carlist.clear()
print(carlist)

Output:
[]

List Comprehension in Python

Sometimes we want to create a new list from an existing list based on multiple conditions then it requires multiple lines of code to be written. In order to display one line of code, we use list comprehension by combining those multiple lines of code. it has the following form

newlist = [ expression for item in iterable if condition == True]

Below is an example to print all the items of a list using for loop and extract those items based on the presence of a particular alphabet such as ‘o’.

Example 19:-

carlist = ["maruti","volvo","BMW","Toyota"]
newcarlist = []
for x in carlist:
   if 'o' in x:
      newcarlist.append(x)
print(newcarlist)

You can write the above code using List comprehension as shown below

newcarlist = [x for x in carlist if 'o' in x]
print(newcarlist)

Output:
['volvo', 'Toyota']

Sorting list in ascending order in Python

You can sort the items in a list using the sort() function in Python as shown below

Example 20:-

carlist = ["maruti","volvo","BMW","Toyota"]
carlist.sort()
print(carlist)

Output:
['BMW', 'Toyota', 'maruti', 'volvo']

Sorting List in descending order in Python

You can sort the items in a list in descending order in Python as shown below

Example 21:-

carlist = ["maruti","volvo","BMW","Toyota"]
carlist.sort(reverse = True)
print(carlist)

Output:
['volvo', 'maruti', 'Toyota', 'BMW']

Copy list in Python

We can copy the items of an existing list and display all the items in a new list as shown below

Example 22:-

carlist = ["maruti","volvo","BMW","Toyota"]
newcarlist = carlist.copy()
print(newcarlist)

Output:
['maruti', 'volvo', 'BMW', 'Toyota']

You can download the python latest version from here

Conclusion:- I hope this tutorial will help you to understand the concept. If there is any doubt then please leave a comment below


Leave a Comment

THOR311

ceri188

ceri188

ceri188

KUPU178

HOKI311

RATU311

KUPU178

RATU311

RATU311

kupu178

kupu178

HOKI311

togel

kupu178

ratu311

mix parlay

ceri188

slot online

kupu178

KUPU178

RATU311

THOR311

THOR311

KUPU178

KUPU178

KUPU178

RATU311

RATU311

RATU311

kupu178

hoki311

THOR311

hoki311

togel online

kupu178

slot mahjong

ceri188

slot server thailand

ratu311

ceri188

ceri188

RATU311

punjabi virasat

mimcord.com

Centrum Medyczne

www.vrcorporate.in

phbalance.vn

KUPU178

daftar slot gacor

hoki311

togel hongkong

kupu178

slot mahjong

ratu311

slot gacor

kupu178

kupu178

ceri188

kupu178

ceri188

KUPU178

KUPU178

KUPU178

RATU311

kupu178

RATU311

RATU311

RATU311

KUPU178

KUPU178

RATU311

RATU311

slot online

kupu178

thor311

thor311

kupu178

kupu178

RATU311

KUPU178

THOR311

THOR311

KUPU178

slot mahjong ways

hoki311

slot gacor

ceri188

ratu311

kupu178

Casino Baccarat Online

kupu178

kupu178

ceri188

ceri188

THOR311

RATU311

toto macau

KUPU178

slot gacor

HOKI311

sabung ayam online

RATU311

SLOT LUCKY NEKO

CERI188

Slot Gacor

KUPU178

RATU311

RATU311

Link Spaceman

CERI188

Slot Gacor Online

THOR311

Mahjong ways

KUPU178

THOR311

THOR311

Live Casino

HOKI311

CERI188

CERI188

THOR311

THOR311

THOR311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

KUPU178

CERI188

HOKI311 SLOT

TOTO TOGEL TOKYO

CERI188

CERI188 SITE

CERI188

RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

Toto Togel

KUPU178

Slot Mahjong

CERI188

CERI188

CERI188

Slot Sweet Bonanza 2500

KUPU178

SLOT ONLINE

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

KUPU178 X SLOT777

judi bola online

THOR311

KUPU178 10 GAME SLOT TERBAIK

KUPU178

KUPU178 SLOT TERPERCAYA

KUPU178 TOGEL

TOTO 4D MACAU

KUPU178

SITUS BOLA

CERI188

KUPU178

RATU311

THOR311

HOKI311

KUPU178

RATU311

THOR311

CERI188

Slot Mahjong Ways

KUPU178

CERI188

Slot Online Resmi

KUPU178

RATU311 SLOT ONLINE

KUPU178

KUPU178: Link Akses Resmi

KUPU178: Strategi Menang Di Game Live Casino

KUPU178

KUPU178: Minimal 200 Rupiah

Slot Gacor Online

KUPU178

Mix Parlay

CERI188

CERI188

THOR311

THOR311

THOR311

THOR311

THOR311

KUPU178

Slot Thailand

CERI188

CERI188

THOR311 RTP

RATU311

RATU311

RATU311

CERI188

KUPU178 TOTO SLOT

CERI188 TOTO Singapore

THOR311

KUPU178

KUPU178

RATU311

CERI188

RATU311: Alternatif karnpuracollege

RATU311

RATU311

Thor311 Terbaru

THOR311 MAHJONG

RATU311: BONUS SABUNG AYAM

CERI188: Masuk Akun Slot Super Cepat

THOR311 PRO

CERI188

CERI188

CERI188

THOR311

HOKI311

HOKI311

RATU311

HOKI311

KUPU178

RATU311

RATU311

RATU311

RATU311

THOR311

CERI188

CERI188

RATU311

RATU311

RATU311

RATU311

Game Thor311

HOKI311

HOKI311

RATU311

HOKI311

HOKI311

THOR311

RATU311 NONTON BOLA

RATU311

HOKI311 Link QRIS EWALLET

RATU311

HOKI311 Link Apk Resmi

CERI188

RATU311 judi bola

KUPU178

situs judi bola

HOKI311

RATU311

THOR311

HOKI311

KUPU178

kupu178 link alternatif

kupu178 slot

kupu178 daftar

kupu178 login

link daftar kupu178

judi bola

RATU311

link alternatif RATU311

Link daftar RATU311

link login RATU311

RATU311

KUPU178

KUPU178

RATU311

RATU311

KUPU178

THOR311

slot thailand

judi bola

KUPU178

slot gacor

slot gacor

yakuza303

slot gacor

RATU311

THOR311