Python-Ref > Data structures > List > What is a list
 
 

^^->
Klíčová slova
Moduly
Knihovní funkce

What is a list

Lists, like tuples or strings belong to the class of sequence types. Unlike tuples or strings, lists are mutable. This means that you can change the content of a list during your work.
Lists are usually used for storage of multiple values of the same type (although you can also put different types of data into lists).
To create a list, you simply put square brackets around the values separated by a coma. Lists may contain all kinds of data, even other lists.
Lists support all of the functions common for all sequence types.
Expand/Shrink
Zdroj: (list1-1.py)
  1   list1 = [1,2,3,4,10]   #list of integers
  2   list2 = ["hi", "there", "how", "are", "you?"]  # list of strings
  3   list3 = [list2, list1[-1]]   # list containing list2 and last item of list1
  4   
  5   print list1
  6   print list2[1]   # second item of list2
  7   print list3
stdout:
[1, 2, 3, 4, 10]
there
[['hi', 'there', 'how', 'are', 'you?'], 10]
Doba běhu: 21.1 ms
Lists of integers are conveniently created using the builtin range function.
Expand/Shrink
Zdroj: (list1-2.py)
  1   xs = range( 10)   # integers from 0 to 9
  2   ys = range( 10, 20)   # integers from 10 to 19
  3   zs = range( 10, 100, 10)   # integers from 10 to 90 with step 10
  4   
  5   print xs
  6   print ys
  7   print zs
  8   
  9   print xs[0] + ys[1] + zs[2]
stdout:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
[10, 20, 30, 40, 50, 60, 70, 80, 90]
41
Doba běhu: 21.4 ms