• Latest
  • Trending
Python Initialize List – How to do it?

Python Initialize List – How to do it?

June 30, 2022
Co-Creation Hub’s edtech accelerator puts $15M towards African startups

Co-Creation Hub’s edtech accelerator puts $15M towards African startups

February 20, 2023
Data Leak Hits Thousands of NHS Workers

Data Leak Hits Thousands of NHS Workers

February 20, 2023
EU Cybersecurity Agency Warns Against Chinese APTs

EU Cybersecurity Agency Warns Against Chinese APTs

February 20, 2023
How Your Storage System Will Still Be Viable in 5 Years’ Time?

How Your Storage System Will Still Be Viable in 5 Years’ Time?

February 20, 2023
The Broken Promises From Cybersecurity Vendors

Cloud Infrastructure Used By WIP26 For Espionage Attacks on Telcos

February 20, 2023
Instagram and Facebook to get paid-for verification

Instagram and Facebook to get paid-for verification

February 20, 2023
YouTube CEO Susan Wojcicki steps down after nine years

YouTube CEO Susan Wojcicki steps down after nine years

February 20, 2023
Inaugural AfCFTA Conference on Women and Youth in Trade

Inaugural AfCFTA Conference on Women and Youth in Trade

September 6, 2022
Instagram fined €405m over children’s data privacy

Instagram fined €405m over children’s data privacy

September 6, 2022
8 Most Common Causes of a Data Breach

5.7bn data entries found exposed on Chinese VPN

August 18, 2022
Fibre optic interconnection linking Cameroon and Congo now operational

Fibre optic interconnection linking Cameroon and Congo now operational

July 15, 2022
Ericsson and MTN Rwandacell Discuss their Long-Term Partnership

Ericsson and MTN Rwandacell Discuss their Long-Term Partnership

July 15, 2022
  • Consumer Watch
  • Kids Page
  • Directory
  • Events
  • Reviews
Wednesday, 29 March, 2023
  • Login
itechnewsonline.com
  • Home
  • Tech
  • Africa Tech
  • InfoSEC
  • Data Science
  • Data Storage
  • Business
  • Opinion
Subscription
Advertise
No Result
View All Result
itechnewsonline.com
No Result
View All Result

Python Initialize List – How to do it?

by ITECHNEWS
June 30, 2022
in Data Science, Leading Stories
0 0
0
Python Initialize List – How to do it?

Python List is a collection of ordered values and can hold any type of value. It is a container where we can insert, delete or modify the existing values as the list is mutable in Python. Unlike sets, the list can hold the same value multiple times and considers each as a separate item. In this tutorial, we will read how to initialize a list in Python.

Introduction to Python Initialize List

In Python, a user can perform a single task using different ways. Similarly, in order to initialize a list in Python, there is more than one way to do that and we will discuss a few in this tutorial.

YOU MAY ALSO LIKE

Co-Creation Hub’s edtech accelerator puts $15M towards African startups

Data Leak Hits Thousands of NHS Workers

 

Initialize lists using the square brackets in Python

If you want to create an empty list with no values in Python, the square bracket is the way to initialize the list with empty values. You just have to declare a non-value list by specifying a set of square brackets without item values.

Input:

list = []
print (list)

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

 

Output:

[]
[1,2,3,4]

Using the list() function to initialize the list in Python

It is another way to create an empty list without values in Python as the list() function creates the list from the iterable object.

An iterable may be a container, a sequence that supports iteration, or an iterator object. If no parameter is specified, then a new empty list is created.

Input:

list_1 = list()
print (list_1)

res = []
print (data == res)

 

Output:

[]
TRUE

Since the square bracket approach is more instructive and concise, it is preferred over the list() function approach.

Using comprehensions to initialize the list in Python

In order to initialize the list with default values, we can use the comprehension method. It consists of square brackets containing an expression followed by a for clause and further followed by an optional if clause. The expression can be any type of object that you want to put on the list. Since the user is initializing the list with zeros, the expression will just be 0.

List comprehension is an easy way to define a list based on an iterator because it is elegant, simple, and widely recognized.

Input:

list = [i for i in range(5)]
print (list)

 

Output:

[0,1,2,3,4]

This method is way faster than using for and while loops to initialize lists in Python.

Initialize lists using the * operator in Python

Another approach to initialize the list with multiple values is the * operator. The syntax is [object]*n where n is the no of elements in the array.

This method allows us to create a list with a specific number of predefined values.

Input:

list = [5]*10
print (list)

 

Output:

[5,5,5,5,5,5,5,5,5,5]

This is the fastest method among all the methods discussed to initialize the list in Python.

The only drawback of using the * operator to initialize a list in Python is while declaring 2d arrays as it creates shallow lists i.e only one list object would be created and all the indices would refer to this object which can be inconvenient. That is why in the case of 2d arrays, comprehensions are the better way to initialize a list.

Closing thoughts

Initializing the list is one of the basic things to know while working with lists. We have discussed square brackets and the list() function to create empty lists and comprehensions and the * operator to create a list with multiple values. One can learn about other Python concepts here.

Source: hrishikesh1990
Tags: Python Initialize List - How to do it?
ShareTweetShare
Plugin Install : Subscribe Push Notification need OneSignal plugin to be installed.

Search

No Result
View All Result

Recent News

Co-Creation Hub’s edtech accelerator puts $15M towards African startups

Co-Creation Hub’s edtech accelerator puts $15M towards African startups

February 20, 2023
Data Leak Hits Thousands of NHS Workers

Data Leak Hits Thousands of NHS Workers

February 20, 2023
EU Cybersecurity Agency Warns Against Chinese APTs

EU Cybersecurity Agency Warns Against Chinese APTs

February 20, 2023

About What We Do

itechnewsonline.com

We bring you the best Premium Tech News.

Recent News With Image

Co-Creation Hub’s edtech accelerator puts $15M towards African startups

Co-Creation Hub’s edtech accelerator puts $15M towards African startups

February 20, 2023
Data Leak Hits Thousands of NHS Workers

Data Leak Hits Thousands of NHS Workers

February 20, 2023

Recent News

  • Co-Creation Hub’s edtech accelerator puts $15M towards African startups February 20, 2023
  • Data Leak Hits Thousands of NHS Workers February 20, 2023
  • EU Cybersecurity Agency Warns Against Chinese APTs February 20, 2023
  • How Your Storage System Will Still Be Viable in 5 Years’ Time? February 20, 2023
  • Home
  • InfoSec
  • Opinion
  • Africa Tech
  • Data Storage

© 2021-2022 iTechNewsOnline.Com - Powered by BackUPDataSystems

No Result
View All Result
  • Home
  • Tech
  • Africa Tech
  • InfoSEC
  • Data Science
  • Data Storage
  • Business
  • Opinion

© 2021-2022 iTechNewsOnline.Com - Powered by BackUPDataSystems

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In
Go to mobile version