Lists in Python

The list type is probably the most commonly used collection type in Python. Despite its name, a list is more like an array in other languages, mostly JavaScript. In Python, a list is merely an ordered collection of valid Python values. A list can be created by enclosing values, separated by commas, in square brackets:


list = [123,'abcd',10.2,'d'] #can be an array of any data type or single data type.
list1 = ['hello','world']
print(list) #will output whole list.
[123,'abcd',10.2,'d']

print(list[0:2]) #will output first two element of list.
 [123,'abcd']

print(list1 * 2) #will gave list1 two times.
['hello','world','hello','world']

print(list + list1) #will gave concatenation of both the lists.
[123,'abcd',10.2,'d','hello','world']



int_list = [1, 2, 3]
string_list = ['abc', 'defghi']

A list can be empty:

empty_list = []

The elements of a list are not restricted to a single data type, which makes sense given that Python is a dynamic language:

mixed_list = [1, 'abc', True, 2.34, None]

A list can contain another list as its element:

nested_list = [['a', 'b', 'c'], [1, 2, 3]]

The elements of a list can be accessed via an index, or numeric representation of their position. Lists in Python are zero-indexed meaning that the first element in the list is at index 0, the second element is at index 1 and so on:

names = ['Alice', 'Bob', 'Craig', 'Diana', 'Eric']

print(names[0]) # Alice

print(names[2]) # Craig

Indices can also be negative which means counting from the end of the list (-1 being the index of the last element). So, using the list from the above example:
print(names[-1]) # Eric
print(names[-4]) # Bob

Lists are mutable, so you can change the values in a list:

names[0] = 'Ann'
print(names) # Outputs ['Ann', 'Bob', 'Craig', 'Diana', 'Eric']

Besides, it is possible to add and/or remove elements from a list:

Append object to end of list with L.append(object), returns None.

names = ['Alice', 'Bob', 'Craig', 'Diana', 'Eric']

names.append("Sia")

print(names) # Outputs ['Alice', 'Bob', 'Craig', 'Diana', 'Eric', 'Sia']


Add a new element to list at a specific index. L.insert(index, object)

names.insert(1, "Nikki")

print(names) # Outputs ['Alice', 'Nikki', 'Bob', 'Craig', 'Diana', 'Eric', 'Sia']

Remove the first occurrence of a value with L.remove(value), returns None

 names.remove("Bob")

print(names) # Outputs ['Alice', 'Nikki', 'Craig', 'Diana', 'Eric', 'Sia']

Get the index in the list of the first item whose value is x. It will show an error if there is no such item.

name.index("Alice")
 0


Count length of list
len(names)
6

count occurrence of any item in list
a = [1, 1, 1, 2, 3, 4]
a.count(1)
3


Reverse the list

a.reverse() [4, 3, 2, 1, 1, 1]
 # or
a[::-1] [4, 3, 2, 1, 1, 1]

 Remove and return item at index (defaults to the last item) with L.pop([index]),
returns the item

names.pop() # Outputs 'Sia'

You can iterate over the list elements like below:

for element in my_list:
            print (element)

Comments :

Post a Comment