Creating a List in Python

Lists in Python can be created by just placing the sequence inside the square brackets[]. Unlike Sets, a list doesn’t need a built-in function for its creation of a list. 

Note: Unlike Sets, the list may contain mutable elements.  

Example 1: Creating a list in Python

Python
# 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 = ["Geeks", "For", "Geeks"]
print("\nList Items: ")
print(List[0])
print(List[2])

Output
Blank List: 
[]

List of numbers: 
[10, 20, 14]

List Items: 
Geeks
Geeks

Complexities for Creating Lists

Time Complexity: O(1)

Space Complexity: O(n)

Example 2:  Creating a list with multiple distinct or duplicate elements

A list may contain duplicate values with their distinct positions and hence, multiple distinct or duplicate values can be passed as a sequence at the time of list creation.

Python
# Creating a List with
# the use of Numbers
# (Having duplicate values)
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, 'Geeks', 4, 'For', 6, 'Geeks']
print("\nList with the use of Mixed Values: ")
print(List)

Output
List with the use of Numbers: 
[1, 2, 4, 4, 3, 3, 3, 6, 5]

List with the use of Mixed Values: 
[1, 2, 'Geeks', 4, 'For', 6, 'Geeks']

Python Lists

Python Lists are just like dynamically sized arrays, declared in other languages (vector in C++ and ArrayList in Java). In simple language, a list is a collection of things, enclosed in [ ] and separated by commas. 

The list is a sequence data type which is used to store the collection of data. Tuples and String are other types of sequence data types.

Similar Reads

Example of the list in Python

Here we are creating a Python List using []....

Creating a List in Python

Lists in Python can be created by just placing the sequence inside the square brackets[]. Unlike Sets, a list doesn’t need a built-in function for its creation of a list....

Accessing elements from the List

In order to access the list items refer to the index number. Use the index operator [ ] to access an item in a list. The index must be an integer. Nested lists are accessed using nested indexing....

Getting the size of Python list

Python len() is used to get the length of the list....

Taking Input of a Python List

We can take the input of a list of elements as string, integer, float, etc. But the default one is a string....

Adding Elements to a Python List

Method 1: Using append() method...

Reversing a List

Method 1:  A list can be reversed by using the reverse() method in Python....

Removing Elements from the List

Method 1: Using remove() method...

Slicing of a List

We can get substrings and sublists using a slice. In Python List, there are multiple ways to print the whole list with all the elements, but to print a specific range of elements from the list, we use the Slice operation....

List Comprehension

Python List comprehensions are used for creating new lists from other iterables like tuples, strings, arrays, lists, etc. A list comprehension consists of brackets containing the expression, which is executed for each element along with the for loop to iterate over each element....

Basic Example on Python List

Python program to interchange first and last elements in a listPython program to swap two elements in a listPython – Swap elements in String listPython | Ways to find length of listMaximum of two numbers in PythonMinimum of two numbers in Python...

List Methods

FunctionDescriptionAppend()Add an element to the end of the listExtend()Add all elements of a list to another listInsert()Insert an item at the defined indexRemove()Removes an item from the listClear()Removes all items from the listIndex()Returns the index of the first matched itemCount()Returns the count of the number of items passed as an argumentSort()Sort items in a list in ascending orderReverse()Reverse the order of items in the listcopy()Returns a copy of the listpop()Removes and returns the item at the specified index. If no index is provided, it removes and returns the last item....