Python in Plain English

New Python content every day. Follow to join our 3.5M+ monthly readers.

Follow publication

An Introduction to Lists in Python for Beginners

Rohit Kumar Thakur
Python in Plain English
3 min readNov 14, 2021

--

Python Tutorial Series

Hello, Python Enthusiasts!

In the last section of this Python Tutorial Series, we talked about the processing and reversing of strings in Python. In this article, we are going to understand Python Lists with some examples. If you are following this Python Tutorial Series, then follow and share the article with your developer community.

Attention all developers seeking to make social connections and establish themselves while earning passive income — look no further! I highly recommend ‘From Code to Connections’, a book that will guide you through the process. Don’t miss out, grab your copy now on Amazon worldwide or Amazon India! You can also go for Gumroad

What are Lists in Python? A list is an ordered sequence of values. It is a non-scalar type. You can store any type of value in a list such as an integer, string, float, or list. Lists are mutable, and therefore, one can modify individual elements of a list. Let’s understand all this with an example.

>>> players =['Dhoni', 'Kohli', 'Rohit', 'Sachin', 'Raina']

In the above example, we are storing the player’s name in a list. Note that the elements of a list are enclosed in square brackets, separated by commas. The first element of the list is indexed as 0, just like the characters of the string. Something just like this:

index         element0              Dhoni
1 Kohli
2 Rohit
3 Sachin
4 Raina

Now, you know that the lists are mutable, and therefore, you can modify individual elements of a list. Let’s understand this with an example. Above we have a list of players. I want to change the name of the player at index 2 to “Bumrah”. You can do this using the following way:

>>> players[2] = 'Bumrah'
>>> print(players)
['Dhoni', 'Kohli', 'Bumrah', 'Sachin', 'Raina']

Next, we create a list of players and their respective scores. For this purpose, we represent each pair of players and the player’s score as a list and form a list of such lists. This type of list is called a two-dimensional list. Let’s understand this with an example.

>>> players = [['Dhoni', 183], ['Kohli', 161], ['Rohit', 264], ['Sachin',200]]

Now, let suppose you want to change the individual score of “Kohli” to 183. You can do that using the following way:

>>> players[1][1] = 183
>>> print(players)
[['Dhoni', 183], ['Kohli', 183], ['Rohit', 264], ['Sachin', 200]]

More List Operations

You can perform multiple operations on a list. Let’s understand this with examples.

>>> list1 = [10,20,30,40,50,60]
>>> list2 = ['Red', 'Black', 'Blue', 'Green']

Concatenation

>>> list3 = list1+list2
>>> list3
[10, 20, 30, 40, 50, 60, 'Red', 'Black', 'Blue', 'Green']

Multiplication

>>> list1*3
[10, 20, 30, 40, 50, 60, 10, 20, 30, 40, 50, 60, 10, 20, 30, 40, 50, 60]

Finding the Length of a List

>>> len(list2)
4

Indexing

>>> list1[-2]
50

Slicing

>>> list1[1:4]
[20, 30, 40]

min() Function

>>> min(list2)
'Black'

max() Function

>>> max(list1)
60

sum() Function

>>> sum(list1)
210

Member Operator in

>>> 35 in list1
False
>>> 'Blue' in list2
True

You can also iterate over the element of a list.

>>> list2 = ['Red', 'Black', 'Blue', 'Green']
>>> for color in list2:
print(color)
Red
Black
Blue
Green

Well, that’s it for this article. In the next article, we will look after how the built-in functions perform on a list.

If this article sounds informative to you then make sure to follow and clap. Share it with your geek community.

Thanks for reading.

Hello, My Name is Rohit Kumar Thakur. I am open to freelancing. I build React Native projects and I am currently working on Python Django. Feel free to contact me at (freelance.rohit7@gmail.com).

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Published in Python in Plain English

New Python content every day. Follow to join our 3.5M+ monthly readers.

No responses yet

Write a response