Follow

Python Lists

List is used to store multiple values in single variable, list value inclosed in square[].A list is structure data in python it is mutable(changeable) or ordered of values.
  • store multiple value in single variable.
  • list is mutable(changeable).
  • collection of ordered elements.
  • allow duplicate values.
  • list is one in 4 data structure.

Python list

x=["apple", "banana", "guava"]
print(x)

Try it Yourself »

Output

['apple','banana','guava']

List length

check the length of list and who many items in list.

list length

x=["apple", "banana", "guava"]
print(len(x))

Try it Yourself »

Output

3

Data type

list data type

Data type - list

x=["apple", "banana", "guava"]
print(type(x))

Try it Yourself »

Output

<class 'list'>

list creation

Create a list from many ways like array, tuple, string etc.
create a list using tuple

create list using tuple

x=list(1, 2, 3)
print(x)

Try it Yourself »

Output

[1,2,3]
create a list using strings

create list using string

x="hallo"
y=list(x)
print(y)

Try it Yourself »

Output

['h', 'a', 'l', 'l', 'o']

Access item (slicing)

Access elements from list using index number
index of frist element is zero(0). python start indexing from zero(0) to N-1

Access item

x=(1, 2, 3,4,5,6,7)
print(x[0])
print(x[-1])
#slicing
print(x[2:5])
#negative slicing
print(x[-7:-2])

Try it Yourself »

Output

[1]
[7]
[3, 4, 5]
[1, 2, 3, 4, 5]

Python arrays

  • Tuple:tuple is written in Parentheses (), collection of immutable and ordered values. allow duplicate.
  • List:tuple is written in square brackets [], collection of mutavle and ordered values. allow duplicate.
  • Dict:tuple is written in curly brackets {}, collection of unordered and mutable values. not allow duplicate.
  • Set:tuple is written in curly brackets {}, collection of unordered , unindexed and immutable values. not allow duplicate.

No comments:

Post a Comment

Tell us how you like it.