Skip to main content

Python List

python list

Hi everyone. Welcome back to another tutorial on this platform. If you are new here, please check out our previous tutorials on python and subscribe to our YouTube for more video tutorials. 

Today we will be talking on python list. As we earlier saw in python arrays, a list is an input data-type that can hold one data type or a combination of several data type. They are created with the use of square brackets and commas to separate the items within it.  

Syntax for a python list

My list=[]

Some examples of python list are

Num_list=[0,5,10,25]

String_list[“cat”, “dog”, ”lion”]

Nested _list=[“keboard”,8,5,9[5,6,1]]

Accessing elements in a python list

They are several methods in accessing elements in a python list, most of which have been mentioned in python array under built-in functions. In addition to what we have seen so far, we are going to see some important ways of accessing elements in a python list.

Changing elements in a list

You can change an item or a range of items in a list with the use of assignment operator(=) and the indexing operator[].

Example:

even=[1,3,5,7,9]
even[0]=2
print(even)

OUTPUT

python list
Concatenating and repeating lists

In this case we are going to add two lists, using the python +operator. 

Example:

animals=['dog','cat','hen']
babies=['puppy','kitchen','chicks']
print(animals+babies)

OUTPUT

python list
In addition to adding of two lists, you can use the *operator to repeat a list a number times.

Example

abc=['a','b','c']*3
print(abc)

OUTPUT

python list
Inserting items

This is different from the append () method that we learned earlier. In this method, you will learn how to insert an item on your desired location with the insert() method

Here’s the syntax for the insert() method

List.insert(index,obj)

Index: where the object ‘obj’ need to be inserted
Obj: this is the object to be inserted into the given list

Example

numbers=[1,2,4,6,8,10]
numbers.insert(2,3)
print(numbers)

In the case given, we want to insert 3 before 2, so we have to put 2 before 3, as the compiler will run it as 2 before of 3

OUPUT

python list
Testing membership on a list

These are mostly operators which are found on a list and are of two types “in” and “not in”. After the evaluation, the compiler returns the results as true or false.  Here are examples in integers.

my_numbers=[1,2,5,7,36,48,]
print(7 in my_numbers)
print(14 in my_numbers)

OUTPUT

python list
List comprehension

It is a way of creating a new list from an existing list. It is made up of an expression and a for ‘for statement’ enclosed in a square brackets.  To illustrate, here is an example of creating a list where each item is an increasing power of 3.

pow3=[3**y for y in range(6)]
print(pow3)

OUPUT

python list

Comments

Popular posts from this blog

Introduction to flask

Hello. Welcome to another session on this platform. If you are new here, please do checkout our previous articles on python programming language and stay excited on this session because we are entering into one of python’s web-based application called flask. In this article, we are going to see the following What is flask Prequistes Installation of flask in python Some of flask template engine. What is flask? Flask is one of python-based framework which is used to create web applications. Flight is a very light web framework. During installation, flask comes with pre-installed modules and functions which are used to backup your own web applications. Flask is very easy to understand and perfect go-to for beginners and with flask, a web server can run in less than 3 lines of code. Prequistes Before learning flask, we recommend the reader to have a basic mastery of python concepts. Installation of flask  Before installing flask, we have to checked if python has been installed or. If n...

How to generate random numbers using NumP1

Hello. Welcome to another edition on this platform. For more better understanding, do checkout our YouTube channel to get the video tutorial. In this article of today, we are going to see how to generate random numbers using any of the following methods: Generating a random number Generating a random float and integer Generating random number arrays Generating random number from an array What is a random number? This is a number which cannot be predicted before its occurrence. This number might not different every time. Programmatically, they are two categories of random numbers:     Pseudo-Random numbers       True Random numbers. Just as programs which are written by programmers are a set of instructions, we must follow an algorithm to generate random numbers. Random numbers which are generated using an algorithm are called Pseudo-Random numbers. To generate a true random number, it is important to get the data from sources such as the keyboards, mou...

Introduction to Django

Hello. Welcome to another session on this platform. If you are new here, please do checkout our previous articles on python programming language and stay excited on this session. we are entering into one of python’s application called Django. In this article, we are going to discuss the following: What is Django Why must we use Django  A brief history of Django Installation of Django Popularity of Django What is Django? Python has so many framework application and Django happen to be one of them. Being a python-based-framework, it is used to quickly create web applications.  When building websites, django provides similar ready-made components to handle user authentication, forms, a way to upload components. Why use django? Django is very fast. It takes applications from concept to applications very quickly Django has thousand available packages in it when it is installed. With django, we can launch web applications is a matter of hours. Django is a highly is secured and helps...