complete the list programming assignment . complete the dictionary programming assignment.
HTML and CSS
Learn HTML Learn CSS Learn Bootstrap Learn W3.CSS Learn Colors Learn Icons Learn Graphics Learn SVG Learn Canvas Learn How To Learn SassXML
Learn XML Learn XML AJAX Learn XML DOM Learn XML DTD Learn XML Schema Learn XSLT Learn XPath Learn XQueryJavaScript
Learn JavaScript Learn jQuery Learn React Learn AngularJS Learn JSON Learn AJAX Learn W3.JSProgramming
Learn Python Learn Java Learn C++Server Side
Learn SQL Learn PHP Learn ASP Learn Node.js Learn Raspberry PiWeb Building
Web Templates Web Statistics Web Certificates Web Editor Web DevelopmentXML
Learn XML Learn XML AJAX Learn XML DOM Learn XML DTD Learn XML Schema Learn XSLT Learn XPath Learn XQuery ×HTML
HTML Tag Reference HTML Event Reference HTML Color Reference HTML Attribute Reference HTML Canvas Reference HTML SVG Reference HTML Character Sets Google Maps ReferenceCSS
CSS Reference CSS Browser Support CSS Selector Reference Bootstrap 3 Reference Bootstrap 4 Reference W3.CSS Reference Icon Reference Sass ReferenceJavaScript
JavaScript Reference HTML DOM Reference jQuery Reference AngularJS Reference W3.JS ReferenceProgramming
Python Reference Java ReferenceServer Side
SQL Reference PHP Reference ASP ReferenceXML
XML Reference XML Http Reference XSLT Reference XML Schema ReferenceCharacter Sets
HTML Character Sets HTML ASCII HTML ANSI HTML Windows-1252 HTML ISO-8859-1 HTML Symbols HTML UTF-8 ×Exercises
HTML Exercises CSS Exercises JavaScript Exercises SQL Exercises PHP Exercises Python Exercises jQuery Exercises Bootstrap Exercises Java Exercises C++ ExercisesQuizzes
HTML Quiz CSS Quiz JavaScript Quiz SQL Quiz PHP Quiz Python Quiz jQuery Quiz Bootstrap Quiz XML QuizCertificates
HTML Certificate CSS Certificate JavaScript Certificate SQL Certificate PHP Certificate Python Certificate jQuery Certificate Bootstrap Certificate XML CertificatePython Tutorial
Python HOME Python Intro Python Get Started Python Syntax Python Comments Python Variables Python Data Types Python Numbers Python Casting Python Strings Python Booleans Python Operators Python Lists Python Tuples Python Sets Python Dictionaries Python If...Else Python While Loops Python For Loops Python Functions Python Lambda Python Arrays Python Classes/Objects Python Inheritance Python Iterators Python Scope Python Modules Python Dates Python JSON Python RegEx Python PIP Python Try...Except Python Command Input Python String FormattingFile Handling
Python File Handling Python Read Files Python Write/Create Files Python Delete FilesPython MySQL
MySQL Get Started MySQL Create Database MySQL Create Table MySQL Insert MySQL Select MySQL Where MySQL Order By MySQL Delete MySQL Drop Table MySQL Update MySQL Limit MySQL JoinPython MongoDB
MongoDB Get Started MongoDB Create Database MongoDB Create Collection MongoDB Insert MongoDB Find MongoDB Query MongoDB Sort MongoDB Delete MongoDB Drop Collection MongoDB Update MongoDB LimitPython Reference
Python Overview Python Built-in Functions Python String Methods Python List Methods Python Dictionary Methods Python Tuple Methods Python Set Methods Python File Methods Python KeywordsModule Reference
Random Module Requests ModulePython How To
Remove List Duplicates Reverse a StringPython Examples
Python Examples Python Exercises Python Quiz Python CertificatePython Lists
❮ Previous Next ❯Python Collections (Arrays)
There are four collection data types in the Python programming language:
- List is a collection which is ordered and changeable. Allows duplicate members.
- Tuple is a collection which is ordered and unchangeable. Allows duplicate members.
- Set is a collection which is unordered and unindexed. No duplicate members.
- Dictionary is a collection which is unordered, changeable and indexed. No duplicate members.
When choosing a collection type, it is useful to understand the properties of that type. Choosing the right type for a particular data set could mean retention of meaning, and, it could mean an increase in efficiency or security.
List
A list is a collection which is ordered and changeable. In Python lists are written with square brackets.
Example
Create a List:
thislist = ["apple", "banana", "cherry"] print(thislist) Run example »Access Items
You access the list items by referring to the index number:
Example
Print the second item of the list:
thislist = ["apple", "banana", "cherry"] print(thislist[1]) Run example »Negative Indexing
Negative indexing means beginning from the end, -1 refers to the last item, -2 refers to the second last item etc.
Example
Print the last item of the list:
thislist = ["apple", "banana", "cherry"] print(thislist[-1]) Run example »Range of Indexes
You can specify a range of indexes by specifying where to start and where to end the range.
When specifying a range, the return value will be a new list with the specified items.
Example
Return the third, fourth, and fifth item:
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"] print(thislist[2:5]) Run example »Note: The search will start at index 2 (included) and end at index 5 (not included).
Remember that the first item has index 0.
Range of Negative Indexes
Specify negative indexes if you want to start the search from the end of the list:
Example
This example returns the items from index -4 (included) to index -1 (excluded)
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"] print(thislist[-4:-1]) Run example »Change Item Value
To change the value of a specific item, refer to the index number:Example
Change the second item:
thislist = ["apple", "banana", "cherry"] thislist[1] = "blackcurrant" print(thislist) Run example »Loop Through a List
You can loop through the list items by using a for loop:
Example
Print all items in the list, one by one:
thislist = ["apple", "banana", "cherry"] for x in thislist: print(x) Run example »You will learn more about for loops in our Python For Loops Chapter.
Check if Item Exists
To determine if a specified item is present in a list use the in keyword:
Example
Check if "apple" is present in the list:
thislist = ["apple", "banana", "cherry"] if "apple" in thislist: print("Yes, 'apple' is in the fruits list") Run example »List Length
To determine how many items a list has, use the len() method:
Example
Print the number of items in the list:
thislist = ["apple", "banana", "cherry"] print(len(thislist)) Run example »Add Items
To add an item to the end of the list, use the append() method:
Example
Using the append() method to append an item:
thislist = ["apple", "banana", "cherry"] thislist.append("orange") print(thislist) Run example »To add an item at the specified index, use the insert() method:
Example
Insert an item as the second position:
thislist = ["apple", "banana", "cherry"] thislist.insert(1, "orange") print(thislist) Run example »Remove Item
There are several methods to remove items from a list:
Example
The remove() method removes the specified item:
thislist = ["apple", "banana", "cherry"] thislist.remove("banana") print(thislist) Run example »Example
The pop() method removes the specified index, (or the last item if index is not specified):
thislist = ["apple", "banana", "cherry"] thislist.pop() print(thislist) Run example »Example
The del keyword removes the specified index:
thislist = ["apple", "banana", "cherry"] del thislist[0] print(thislist) Run example »Example
The del keyword can also delete the list completely:
thislist = ["apple", "banana", "cherry"] del thislist Run example »Example
The clear() method empties the list:
thislist = ["apple", "banana", "cherry"] thislist.clear() print(thislist) Run example »Copy a List
You cannot copy a list simply by typing list2 = list1, because: list2 will only be a reference to list1, and changes made in list1 will automatically also be made in list2.
There are ways to make a copy, one way is to use the built-in List method copy().
Example
Make a copy of a list with the copy() method:
thislist = ["apple", "banana", "cherry"] mylist = thislist.copy() print(mylist) Run example »Another way to make a copy is to use the built-in method list().
Example
Make a copy of a list with the list() method:
thislist = ["apple", "banana", "cherry"] mylist = list(thislist) print(mylist) Run example »Join Two Lists
There are several ways to join, or concatenate, two or more lists in Python.
One of the easiest ways are by using the + operator.
Example
Join two list:
list1 = ["a", "b" , "c"] list2 = [1, 2, 3] list3 = list1 + list2 print(list3) Run example »Another way to join two lists are by appending all the items from list2 into list1, one by one:
Example
Append list2 into list1:
list1 = ["a", "b" , "c"] list2 = [1, 2, 3] for x in list2: list1.append(x) print(list1) Run example »Or you can use the extend() method, which purpose is to add elements from one list to another list:
Example
Use the extend() method to add list2 at the end of list1:
list1 = ["a", "b" , "c"] list2 = [1, 2, 3] list1.extend(list2) print(list1) Run example »The list() Constructor
It is also possible to use the list() constructor to make a new list.
Example
Using the list() constructor to make a List:
thislist = list(("apple", "banana", "cherry")) # note the double round-brackets print(thislist) Run example »List Methods
Python has a set of built-in methods that you can use on lists.
| Method | Description |
|---|---|
| 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 (or any iterable), to the end of the current list |
| 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 |
Test Yourself With Exercises
Exercise:
Print the second item in the fruits list.
fruits = ["apple", "banana", "cherry"] print()Submit Answer » ❮ Previous Next ❯
COLOR PICKER
HOW TO
Tabs Dropdowns Accordions Side Navigation Top Navigation Modal Boxes Progress Bars Parallax Login Form HTML Includes Google Maps Range Sliders Tooltips Slideshow Filter List Sort ListSHARE
CERTIFICATES
HTML CSS JavaScript SQL Python PHP jQuery Bootstrap XML
Read More » REPORT ERROR PRINT PAGE FORUM ABOUT ×Your Suggestion:
Your E-mail: Page address: Description: Submit ×Thank You For Helping Us!
Your message has been sent to W3Schools.
Top Tutorials
HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial jQuery Tutorial Java Tutorial C++ TutorialTop References
HTML Reference CSS Reference JavaScript Reference SQL Reference Python Reference W3.CSS Reference Bootstrap Reference PHP Reference HTML Colors jQuery Reference Java Reference Angular ReferenceTop Examples
HTML Examples CSS Examples JavaScript Examples How To Examples SQL Examples Python Examples W3.CSS Examples Bootstrap Examples PHP Examples jQuery Examples Java Examples XML ExamplesWeb Certificates
HTML Certificate CSS Certificate JavaScript Certificate SQL Certificate Python Certificate jQuery Certificate PHP Certificate Bootstrap Certificate XML Certificate Get Certified » W3Schools is optimized for learning, testing, and training. Examples might be simplified to improve reading and basic understanding. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 1999-2019 by Refsnes Data. All Rights Reserved. Powered by W3.CSS.