Lists

Creating Lists

Creating lists in Python can take place by just placing the sequence inside the square brackets[]. Furthermore, it is important to understand that a list is unlike a set. This is because a list doesn’t require a built-in function for the creation of a list.

creating lists

                                                                                                                                    Creating Lists

Simple Guide to Creating Lists

When it comes to lists, the most important question is how to create a list in Python. Below is a simple example to help understand the process of creating list in python, a programming language. Here, the creation of two lists in Python will take place.

(1) List of Names – this list will contain strings whose placing is within quotes:

Names = [‘Peter’, ‘Bill’, ‘Samuel’, ‘Ronald’, ‘Jack’]

(2) Age list – this list will have the involvement of numbers (i.e., integers) without quotes:

Age = [25, 18, 55, 30, 23]

Putting everything together, this is how the Python code would appear for the purpose of creating lists in Python:

Names = [‘Peter’, ‘Bill’, ‘Samuel’, ‘Ronald’, ‘Jack’]

Age = [25, 18, 55, 30, 23]

print(Names)

print (Age)

A quick verification of creating lists can take place by adding the type() syntax as follows:

Names = [‘Peter’, ‘Bill’, ‘Samuel’, ‘Ronald’, ‘Jack’]

Age = [25, 18, 55, 30, 23]

print(type(Names))

print (type(Age))

Browse more Topics under Lists

Demonstration of Python List Creation

                    

# Python program to demonstrate

# Creation of List



# Creating a List

List = []

print("Blank List: ")

print(List)



# Creating a List of numbers

List = [10, 20, 14]

print("\nList of numbers: ")

print(List)



# Creating a List of strings and accessing

# using index

List = ["Toppr", "For", "Toppr"]

print("\nList Items: ")

print(List[0])

print(List[2])



# Creating a Multi-Dimensional List

# (By Nesting a list inside a List)

List = [['Toppr', 'For'] , ['Toppr']]

print("\nMulti-Dimensional List: ")

print(List)

 

Output:

                    

Blank List:

[]

List of numbers:

[10, 20, 14]

List Items

Toppr

Toppr



Multi-Dimensional List:

[['Toppr', 'For'], ['Toppr']]

 

Creating Lists with Duplicate Elements

It is possible that a Python list may involve duplicate values with their distinct positions. Therefore, at the time of creating lists, it is possible to pass duplicate elements or multiple distinct elements as a sequence.

                    

# Creating a List with

# the use of Numbers

# (Having duplicate values)

Furthermore, List = [1, 2, 4, 4, 3, 3, 3, 6, 5]

print("\nList with the use of Numbers: ")

print(List)



# Creating a List with

# mixed type of values

# (Having numbers and strings)

List = [1, 2, 'Toppr', 4, 'For', 6, 'Toppr']

print("\nList with the use of Mixed Values: ")

print(List)


Output:

                    

List with the use of Numbers:

Most noteworthy = [1, 2, 4, 4, 3, 3, 3, 6, 5]



List with the use of Mixed Values:

[1, 2, 'Toppr', 4, 'For', 6, 'Toppr']

 

Knowing the size of List

                    

# Creating a List

List1 = []

print(len(List1))



# Creating a List of numbers

List2 = [10, 20, 14]

print(len(List2))

# Creating a List with

# the use of Numbers

# (Having duplicate values)

Furthermre, the List = [1, 2, 4, 4, 3, 3, 3, 6, 5]

print("\nList with the use of Numbers: ")

print(List)



# Creating a List with

# mixed type of values

# (Having numbers and strings)

List = [1, 2, 'Toppr', 4, 'For', 6, 'Toppr']

print("\nList with the use of Mixed Values: ")

print(List)

 

Output:

                    

List with the use of Numbers:

Most noteworthy, [1, 2, 4, 4, 3, 3, 3, 6, 5]



List with the use of Mixed Values:

[1, 2, 'Toppr', 4, 'For', 6, 'Toppr']

 

Knowing the size of List

                    

# Creating a List

List1 = []

print(len(List1))



# Creating a List of numbers

List2 = [10, 20, 14]

print(len(List2)

 

Output:

 

FAQs For Creating Lists

Question 1: Briefly explain the procedure of creating lists in python?

Answer 1: In Python programming, the creation of a list takes place by placing all the items elements inside square brackets []. Furthermore, their separation is by commas. Moreover, a list can have any number of items and can also be of different types (integer, float, string etc.).

Question 2: What is the use of the list() function?

Answer 2: The list() function facilitates the creation of a list object and the syntax associated with this particular function is list(iterable). Moreover, a list object refers to a collection that is changeable as well as ordered. Furthermore, below is the example of the list() Python function:

Create a list containing fruit names:

x = list((‘apple’, ‘banana’, ‘cherry’))

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

avatar
  Subscribe  
Notify of

Customize your course in 30 seconds

Which class are you in?
No thanks.