How To Make A List In Python?

Python records are quite possibly the most adaptable datum types that permit us to work with numerous components without a moment’s delay. For instance,

# a rundown of programming dialects

[‘Python’, ‘C++’, ‘JavaScript’]

Make Python Lists

In Python, a rundown is made by putting components inside square sections [], isolated by commas.

# rundown of whole numbers

my_list = [1, 2, 3]

A rundown can have quite a few things and they might be of various sorts (whole number, float, string, and so forth)

# void rundown

my_list = []

# list with blended information types

my_list = [1, “Hi”, 3.4]

A rundown can likewise have one more rundown as a thing. This is known as a settled rundown.

# settled rundown

my_list = [“mouse”, [8, 4, 6], [‘a’]]

Access List Elements

There are different manners by which we can get to the components of a rundown.

List Index

We can utilize the list administrator [] to get to a thing in a rundown. In Python, files start at 0. In this way, a rundown having 5 components will have a record from 0 to 4.

Attempting to get to files other than these will raise an IndexError. The file should be a number. We can’t utilize float or different sorts, this will bring about TypeError.

Settled records are gotten to utilizing settled ordering.

my_list = [‘p’, ‘r’, ‘o’, ‘b’, ‘e’]

# first thing

print(my_list[0]) # p

# third thing

print(my_list[2]) # o

# fifth thing

print(my_list[4]) # e

# Settled List

n_list = [“Happy”, [2, 0, 1, 5]]

# Settled ordering



# Mistake! No one but number can be utilized for ordering








Traceback (latest call last):

  Record “<string>”, line 21, in <module>

TypeError: list files should be numbers or cuts, not float

Negative ordering

Python permits negative ordering for its successions. The file of – 1 alludes to the last thing, – 2 to the second keep going thing, etc.

# Negative ordering in records

my_list = [‘p’,’r’,’o’,’b’,’e’]

# last thing


# fifth last thing





List Slicing in Python

We can get to a scope of things in a rundown by utilizing the cutting administrator :.

# List cutting in Python

my_list = [‘p’,’r’,’o’,’g’,’r’,’a’,’m’,’i’,’z’]

# components from list 2 to file 4


# components from list 5 to end


# components start to finish



[‘o’, ‘g’, ‘r’]

[‘a’, ‘m’, ‘I’, ‘z’]

[‘p’, ‘r’, ‘o’, ‘g’, ‘r’, ‘a’, ‘m’, ‘I’, ‘z’]

Note: When we cut records, the beginning list is comprehensive however the end file is select. For instance, my_list[2: 5] returns a rundown with components at list 2, 3 and 4, yet not 5.

Add/Change List Elements

Records are impermanent, meaning their components can be changed not normal for string or tuple.

We can utilize the task administrator = to change a thing or a scope of things.

# Remedying botch esteems in a rundown

odd = [2, 4, 6, 8]

# change the first thing

odd[0] = 1


# change second to fourth things

odd[1:4] = [3, 5, 7]



[1, 4, 6, 8]

[1, 3, 5, 7]

We can add one thing to a rundown utilizing the affix() strategy or add a few things utilizing the expand() technique.

# Annexing and Extending records in Python

odd = [1, 3, 5]



odd.extend([9, 11, 13])



[1, 3, 5, 7]

[1, 3, 5, 7, 9, 11, 13]

We can likewise utilize + administrator to consolidate two records. This is likewise called link.

The * administrator rehashes a rundown for the given number of times.

# Linking and rehashing records

odd = [1, 3, 5]

print(odd + [9, 7, 5])

print([“re”] * 3)


[1, 3, 5, 9, 7, 5]

[‘re’, ‘re’, ‘re’]

Besides, we can embed one thing at an ideal area by utilizing the strategy addition() or supplement numerous things by fitting it into an unfilled cut of a rundown.

# Exhibition of rundown embed() technique

odd = [1, 9]



odd[2:2] = [5, 7]



[1, 3, 9]

[1, 3, 5, 7, 9]


Erase List Elements

We can erase at least one things from a rundown utilizing the Python del proclamation. It can even erase the rundown totally.

# Erasing list things

my_list = [‘p’, ‘r’, ‘o’, ‘b’, ‘l’, ‘e’, ‘m’]

# erase one thing

del my_list[2]


# erase various things

del my_list[1:5]


# erase the whole rundown

del my_list

# Blunder: List not characterized



[‘p’, ‘r’, ‘b’, ‘l’, ‘e’, ‘m’]

[‘p’, ‘m’]

Traceback (latest call last):

  Document “<string>”, line 18, in <module>

NameError: name ‘my_list’ isn’t characterized

We can utilize eliminate() to eliminate the given thing or fly() to eliminate a thing at the given record.

The pop() strategy eliminates and returns the last thing on the off chance that the record isn’t given. This assists us with executing records as stacks (first in, last out information structure).

What’s more, on the off chance that we need to exhaust the entire rundown, we can utilize the reasonable() strategy.

my_list = [‘p’,’r’,’o’,’b’,’l’,’e’,’m’]


# Yield: [‘r’, ‘o’, ‘b’, ‘l’, ‘e’, ‘m’]


# Yield: ‘o’


# Yield: [‘r’, ‘b’, ‘l’, ‘e’, ‘m’]


# Yield: ‘m’


# Yield: [‘r’, ‘b’, ‘l’, ‘e’]



# Yield: []



[‘r’, ‘o’, ‘b’, ‘l’, ‘e’, ‘m’]


[‘r’, ‘b’, ‘l’, ‘e’, ‘m’]


[‘r’, ‘b’, ‘l’, ‘e’]


At last, we can likewise erase things in a rundown by allotting an unfilled rundown to a cut of components.

>>> my_list = [‘p’,’r’,’o’,’b’,’l’,’e’,’m’]

>>> my_list[2:3] = []

>>> my_list

[‘p’, ‘r’, ‘b’, ‘l’, ‘e’, ‘m’]

>>> my_list[2:5] = []

>>> my_list

[‘p’, ‘r’, ‘m’]

# Model on Python list techniques

my_list = [3, 8, 1, 6, 8, 8, 4]

# Add ‘a’ as far as possible


# Yield: [3, 8, 1, 6, 0, 8, 4, ‘a’]


# Record of first event of 8

print(my_list.index(8)) # Output: 1

# Include of 8 in the rundown

print(my_list.count(8)) # Output: 3

List Comprehension: Elegant method for making Lists

List understanding is an exquisite and succinct method for making another rundown from a current rundown in Python.

A rundown perception comprises of an articulation followed by for proclamation inside square sections.

Here is a guide to make a rundown with every thing being expanding force of 2.

pow2 = [2 ** x for x in range(10)]



[1, 2, 4, 8, 16, 32, 64, 128, 256, 512]

This code is comparable to:

pow2 = []

for x in range(10):

   pow2.append(2 ** x)

A rundown understanding can alternatively contain something else for or then again if explanations. A discretionary in the event that assertion can sift through things for the new rundown. Here are a few models.

>>> pow2 = [2 ** x for x in range(10) if x > 5]

>>> pow2

[64, 128, 256, 512]

>>> odd = [x for x in range(20) if x % 2 == 1]

>>> odd

[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]

>>> [x+y for x in [‘Python ‘,’C ‘] for y in [‘Language’,’Programming’]]

[‘Python Language’, ‘Python Programming’, ‘C Language’, ‘C Programming’]

List Membership Test

We can test on the off chance that a thing exists in a rundown or not, involving the watchword in.

my_list = [‘p’, ‘r’, ‘o’, ‘b’, ‘l’, ‘e’, ‘m’]

# Yield: True

print(‘p’ in my_list)

# Yield: False

print(‘a’ in my_list)

# Yield: True

print(‘c’ not in my_list)





Repeating Through a List

Utilizing a for circle we can repeat through every thing in a rundown.

for organic product in [‘apple’,’banana’,’mango’]:

    print(“I like”,fruit)


I like apple

I like banana

I like mango

How To Generate A Random Number In Python?
How To Install Matplotlib Python?
How To Install Python On Windows?
How To Install Python?
How To Make A List In Python?
How To Print An Array In Java?
How To Print In Python?
How To Read A File In Python?

Latest Updates

Popular Articles

Related Articles

How To Call A Function In Python?

What are Python Functions? In this section, we initially discover the essentials of Python Functions,...

How To Add To A Dictionary Python?

Python word reference is one of the implicit information types. Word reference components are...

How Does Tiktok Algorithm Work?

TikTok is the web-based media sensation whose 'commonly recognized name status' simply continues to...

How Does The Youtube Algorithm Work?

Individuals all over the planet watch north of 1 billion hours of YouTube recordings...