In this article, you will learn what is Python list, how they are created, slicing of a list, adding or removing elements etc.
How to create a list In Python programming, a list is created by placing all the items "elements" inside separated by commas, square brackets []. It can have any number of items and they may have been of different types [string, integer, float etc.]. A list can also have another list as an item. This is called a nested list.
thislist = ["1", "2", "mango"]
print(thislist)
Output
['1', '2', 'mango']
# list with mixed data types
lst= [1, "Hello", 9.1]
print(lst[1])
Output:
Hello
A list can also have another list as an item. This is called a nested list.
lst = ["mango", [1, 4, 6], ['a']]
print(lst[1])
Output:
[1, 4, 6]
Beginning from the end -1
refers to the last items -2
refers to the second last item etc.
lst = ["banana", "sugar", "mango"]
print(lst[-1])
Output:
mango
Basic List Operations
Lists respond to the * and + operators much like strings, they mean repetition and concatenation here too, except that the result is a new list, not a string.
Python Expression | Result | Description |
---|---|---|
for x in [1, 2, 3, 4]: print x, | 1 2 3 4 | Iteration |
[1, 2, 3] + [4, 5, 6] | [1, 2, 3, 4, 5, 6] | Concatenation |
['Hi!'] * 5 | ['Hi!', 'Hi!', 'Hi!', 'Hi!', 'Hi!'] | Repetition |
4 in [1, 2, 3, 4] | True | Membership |
len([1, 2, 3 ,4]) | 4 | Length |
The slice() function returns a slice object. A slice object is used to slice strings, lists, tuple etc. You can specify where to start the slicing, and where to end. You can also specify the step, which allows you to e.g. slice only every other item.
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 Slice Operation. Slice operation is performed on Lists with the use of a colon(:).
a = ("a", "b", "c", "d", "e", "f", "g", "h", "i")
b = slice(4, 6)
print(a[b])
Output
('e', 'f')
What are tuples in python?
In Python, tuples are written with round brackets. A tuple is a collection that is ordered and unchangeable.
my_tuple = ("apple", "cherry", "banana")
print(my_tuple)
Output
('apple', 'cherry', 'banana')
So, you can access tuple items by referring to the index number.
my_tuple = ("apple", "cherry", "banana","mango")
print(my_tuple[2])
Output
banana
So, you can delete one or more items from a list using the keyword del. It can even delete the list entirely.
# Deleting list items
my_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
# delete one item
del my_list[2]
print(my_list)
# delete multiple items
del my_list[1:5]
print(my_list)
# delete entire list
del my_list
# Error: List not defined
print(my_list)
Output
['a', 'b', 'd', 'e', 'f', 'g'] ['a', 'g']
Method Description
index() Returns the index of the first element with the specified value
insert() Adds an element at the specified position
pop() Removes the element at the specified position
remove() Removes the item with the specified value
reverse() Reverses the order of the list
sort() Sorts the list
append() Adds an element at the end of the list
clear() Removes all the elements from the list
copy() Returns a copy of the list
count() Returns the number of elements with the specified value
extend() Add the elements of a list, to the end of the current list
Use the list() constructor to make a new list.
mylist = list(("apple", "mango", "cherry","banana"))
print(mylist)
Output
['apple', 'mango', 'cherry', 'banana']
Join, or concatenate, two or more lists in Python. One of the easiest ways is by using the + operator.
mylist1 = ["a", "b" , "c"]
mylist2 = [1, 2, 3]
mylist3 = mylist1 + mylist2
print(mylist3)
Output
['a', 'b', 'c', 1, 2, 3]
Another way Append mylist2 into mylist1
mylist1 = ["a", "b" , "c"]
mylist2 = [1, 2, 3]
for x in mylist2:
mylist1.append(x)
print(mylist1)
Output
['a', 'b', 'c', 1, 2, 3]
Use the extend()
method to add mylist2 at the end of mylist1
mylist1 = ["a", "b" , "c","d"]
mylist2 = [1, 2, 3]
mylist1.extend(mylist2)
print(mylist1)
Output
['a', 'b', 'c', 'd', 1, 2, 3]
Use the built-in List method copy().
my_list = ["apple", "mango", "cherry","banana"]
mylist = my_list.copy()
print(mylist)
Output
['apple', 'mango', 'cherry', 'banana']
The clear() method empties the list
mylist = ["apple", "mango", "cherry", "banana"]
mylist.clear()
print(mylist)
Output
[]
There are many methods to remove() items from a list
mylist = ["apple", "mango", "cherry", "banana"]
mylist.remove("mango")
print(mylist)
output
['apple', 'cherry', 'banana']
pop() method removes the specified index
mylist = ["apple", "mango", "cherry", "banana"]
mylist.pop()
print(mylist)
Output
['apple', 'mango', 'cherry']
mylist = ["apple", "mango", "cherry", "banana"]
mylist.pop(2)
print(mylist)
Output
['apple', 'mango', 'banana']
To change the value of a defined item, refer to the index number
Change the third item
mylist = ["apple", "mango", "cherry", "banana"]
mylist[2] = "pen"
print(mylist)
Output
['apple', 'mango', 'pen', 'banana']
A list may contain duplicate values with their distinct positions, multiple distinct or duplicate values can be passed as a sequence at the time of list creation.
# 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, 'minifycode', 4, 'For', 6, 'apple']
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, 'minifycode', 4, 'For', 6, 'apple']
How to create a list In Python programming, a list is created by placing all the items "elements" inside separated by commas, square brackets []. It can have any number of items and they may have been of different types [string , integer, float etc.]. A list can also have another list as an item. This is called a nested list. Python List With Examples Python lists, how they are created, slicing of a list, adding or removing elements.
minify code