HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Python / Python – List

Python – List

In Python, lists are:

  • ordered
  • indexed (indices start at 0)
  • mutable
  • heterogeneous (items in a list need not be of the same type)
  • written as a list of comma-separated values between square brackets
listOfSubjects = ['physics', 'chemistry', "mathematics"]

listOfIds = [0, 1, 2, 3, 4]

miscList = [0, 'one', 2, 'three']

1. Access list items

To access values in lists, use the square brackets in slicing syntax or array index form to get a single item or range of items.

The index values passed can be positive or negative both. A negative index value means start counting from the end of the list.

list [m : n] means sublist start at index m (included) and end at index n (not included).

  • If m is not provided then its value is assumed to be zero.
  • If n is not provided then range is selected till last of list.
ids = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

print( ids[0] )			# 0

print( ids[1:5] )		# [1, 2, 3, 4]

print( ids[ : 3] )		# [0, 1, 2]

print( ids[7 : ] )		# [7, 8, 9]

print( ids[-8:-5] )		# [2, 3, 4]

2. Modily list

To change a specific item in the list, refer to it using it’s index and assign a new value.

charList =  ["a", "b", "c"]

charList [2] = "d"

print (charList)	# ['a', 'b', 'd']

3. Iterate a list

We can loop through the list items by using a for loop.

charList =  ["a", "b", "c"]

for x in charList:
	print(x)

# a
# b
# c

4. Check if a item exists in the list

Use the 'in' keyword to determine if a specified item is present in a list.

charList =  ["a", "b", "c"]

if "a" in charList:
	print("a is present")	# a is present

if "d" in charList:
	print("d is present")
else:
	print("d is NOT present")	# d is NOT present

5. Finding length of the list

Use the len() function to find the length of a given list.

charList =  ["a", "b", "c"]

x = len (charList)

print (x)	# 3

6. Adding items

  • To add an item to the end of the list, use the append(item) method.
  • To add an item at specific index position, use the insert(index, item) method. If index is greater than index length, item is added at the end of the list.
charList =  ["a", "b", "c"]

charList.append("d")	
charList.append("e")

print (charList)		# ['a', 'b', 'c', 'd', 'e']

charList.insert(5, "f")

print (charList)		# ['a', 'b', 'c', 'd', 'e', 'f']

charList.insert(10, "h")	# No error 

print (charList)	# ['a', 'b', 'c', 'd', 'e', 'f', 'h']

7. Removing items

To remove an item from the list, use one of the four ways i.e. remove(), pop(), clear() or del keyword.

7.1. remove()

It removes the specified item by it’s value.

charList =  ["a", "b", "c"]

charList.remove("c")	

print (charList)		# ['a', 'b']

7.2. pop()

It removes the specified item by it’s index. If index is not provided, it removes the last item from the list.

charList =  ["a", "b", "c", "d"]

charList.pop()			# removes 'd' - last item

print (charList)		# ['a', 'b', 'c']

charList.pop(1)			# removes 'b'

print (charList)		# ['a', 'c']

7.3. clear()

It empties the list.

charList =  ["a", "b", "c", "d"]

charList.clear()	

print (charList)		# []

7.4. del keyword

It can be used to remove an item from list by it’s index. We can use it to delete the whole list as well.

charList =  ["a", "b", "c", "d"]

del charList[0]	

print (charList)		# ['b', 'c', 'd']

del charList

print (charList)		# NameError: name 'charList' is not defined

8. Join two lists

We can join two given lists in Python using either "+" operator or extend() function.

charList = ["a", "b", "c"]
numList	= [1, 2, 3]

list1 = charList + numList

print (list1)	# ['a', 'b', 'c', 1, 2, 3]

charList.extend(numList)

print (charList)	# ['a', 'b', 'c', 1, 2, 3]

9. Python list methods

9.1. append()

Adds an element at the end of the list.

charList =  ["a", "b", "c"]

charList.append("d")

print (charList)	# ["a", "b", "c", "d"]

9.2. clear()

Removes all the elements from the list.

charList =  ["a", "b", "c"]

charList.clear()

print (charList)	# []

9.3. copy()

Returns a copy of the list.

charList =  ["a", "b", "c"]

newList = charList.copy()

print (newList)	# ["a", "b", "c"]

9.4. count()

Returns the number of elements with the specified value.

charList =  ["a", "b", "c"]

x = charList.count('a')

print (x)	# 1

9.5. extend()

Add the elements of a list to the end of the current list.

charList = ["a", "b", "c"]
numList	= [1, 2, 3]

charList.extend(numList)

print (charList)	# ['a', 'b', 'c', 1, 2, 3]

9.6. index()

Returns the index of the first element with the specified value.

charList =  ["a", "b", "c"]

x = charList.index('a')

print (x)	# 0

9.7. insert()

Adds an element at the specified position.

charList =  ["a", "b", "c"]

charList.insert(3, 'd')

print (charList)	# ['a', 'b', 'c', 'd']

9.8. pop()

Removes the element at the specified position or end of the list.

charList =  ["a", "b", "c", "d"]

charList.pop()			# removes 'd' - last item

print (charList)		# ['a', 'b', 'c']

charList.pop(1)			# removes 'b'

print (charList)		# ['a', 'c']

9.9. remove()

Removes the item with the specified value.

charList =  ["a", "b", "c", "d"]

charList.remove('d')

print (charList)		# ['a', 'b', 'c']

9.10. reverse()

Reverses the order of items in the list.

charList =  ["a", "b", "c", "d"]

charList.reverse()

print (charList)		# ['d', 'c', 'b', 'a']

9.11. sort()

Sorts the given list in ascending order by default.

charList =  ["a", "c", "b", "d"]

charList.sort()

print (charList)		# ["a", "b", "c", "d"]

Happy Learning !!

Read More:

Python – Lists vs Tuples

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Comments are closed on this article!

Search Tutorials

Python Tutorial

  • Python Introduction
  • Python Install in Sublime
  • Python Keywords
  • Python Comments
  • Python Variables
  • Python Data Types
  • Python Type Conversion
  • Python References
  • Python Examples

Python Flow Control

  • Python if…else
  • Python for Loop
  • Python while Loop
  • Python break
  • Python continue
  • Python pass

Python Datatypes

  • Python Integer
  • Python String
  • Python List
  • Python Tuple
  • Python Set
  • Python Dictionary
  • Python OrderedDict
  • Python MultiDict
  • Python Priority Queue

Python Advanced Topics

  • Python Lists vs Tuples
  • Python Comparing Tuples
  • Python Unpacking Tuples
  • Python CSV Files

Python Libraries

  • Python Httplib2

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces