

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Create and Assign Lists in Python?
We can Create and Assign List by Insert, Append Length, Index, Remove and Extend, etc..
Lists are mutable and the changeble object is enclosed within the square brackets i.e [ ], Lists in python is easy.
Example
list =["Tutorials ","Point", "Pvt","Ltd"] list
Output
['Tutorials ', 'Point', 'Pvt', 'Ltd']
Assign Values in Lists To assign values in lists, use square brackets
Example
list1 = ['physics', 'chemistry', 1997, 2000]; list2 = [1, 2, 3, 4, 5, 6, 7 ]; print ("list1[0]: ", list1[0]) print ("list2[1:5]: ", list2[1:5])
When the above code is executed, Following is the preceding output
Output
list1[0]: physics list2[1:5]: [2, 3, 4, 5]
append() and extend() in python append add its argument/parameter as a single element at the end of a list. the list length will increase by one.
Example
list=['Red','Green','Yellow','Purple','White','Black'] list.append('Orange') print(list)
Output
['Red', 'Green', 'Yellow', 'Purple', 'White', 'Black', 'Orange']
extend() iterates over its parameter adding each element to the list and extending the list length of the list will increase through the iterable argument
Example
list=['Car','Bike','Scooty','Bus','Metro'] list.extend('Car') print(list)
When the above code is executed, Following is the preceding output
Output
['Car', 'Bike', 'Scooty', 'Bus', 'Metro', 'C', 'a', 'r']
Index: Index method can search easily, that expects a value is to be searched
Example
list=['Car','Bike','Scooty','Bus','Metro'] list.index('Scooty')
Output
2
- Related Questions & Answers
- In Python how to create dictionary from two lists?
- How to index and slice lists in Python?
- How to create Definition Lists in HTML5?
- What is correct syntax to create Python lists?
- Python - Ways to create a dictionary of Lists
- How to assign values to variables in Python
- How to create a vector of lists in R?
- How to compare two lists in Python?
- How do we assign values to variables in Python?
- Python program to randomly create N Lists of K size
- How to create an array of linked lists in java?
- How to join list of lists in python?
- How To Do Math With Lists in python ?
- How to assign multiple values to a same variable in Python?
- Assign range of elements to List in Python