How to Create an Array in Python

Arrays are useful and fundamental structures that exist in every high-level language. In Python, arrays are native objects called "lists," and they have a variety of methods associated with each object.

Instructions

    • 1

      Create a List With Text or Numeric Elements. You can create a list with a series of text elements in one line in Python.

      List out the text elements you want, separated by commas:
      my_array = ['rebecca', 'juan', 'samir', 'heather']
      You may also use numbers in the array:
      my_array = [-1,0,1,2]
      Or you can mix and match letters and numbers:
      my_array = [1, 'rebecca', 'allard', 15]

    • 2

      Access the values of an array by using an index.

      Know that this index is 0 based, meaning that the first element of the array is referenced by using position 0, the second element of the list is referenced by using position 1, and so forth:
      print my_array[2]
      >>>> samir

    • 3

      Use a list as a dictionary. You can use a dictionary to look up name value pairs for quick retrieval.

      Use a dictionary to look up last names associated with a first name. For example:
      my_dic['rebecca'] = 'allard'
      my_dic['juan'] = 'hernandez'
      my_dic['heather'] = 'aston'

      You can then use the dictionary to print out the value (the last name) using the key (the first name):
      print my_dic['rebecca']
      >>>> allard

    • 4

      Nest a list--realize that arrays in Python can contain any data type, including other arrays.

      Create a list that contains another by simply inserting it into the array element list. For example:
      my_friends = ['rebecca', 'ben', 'biella', 'kevin']
      my_contacts = ['steve', my_friends, 'lee']

      You can now use my_contacts as a normal array:
      print my_contacts[0]
      >>>> steve

      And you can access the nested list by using a second reference:
      print my_contacts[1][2]
      >>>> biella

Related Searches:

Resources

Comments

You May Also Like

  • How to Convert a List to an Array in Python

    Python -- the high-level programming language used by such organizations as Google and NASA -- has the usual kinds of data types:...

  • How to Delete Elements From a Python List

    Python is a versatile high-level programming language that is much more flexible than older languages. One very useful aspect of the programming...

  • How to Append to a List in Python

    The Python programming language stores data in a variety of collections, including a list. The list collection stores a number of items...

  • How to Create a Chart in Python

    Displaying data in the form of charts or graphs in Python requires the use of specialized, external code libraries, and the open-source...

  • How to Create a Python Habitat

    Snakes are expensive pets. The python itself is usually inexpensive-but all the equipment you need to keep the snake alive is not....

  • How to Draw a Five Point Star Using Python Language

    If you use a good graphics library, writing Python programming language that draws five-pointed stars, or any other simple two-dimensional geometric shapes,...

  • How to Create a Tuple in Python

    Python has three types of complex data types called lists, tuples and dictionaries. A tuple is unique because its elements are ordered...

  • How to Initialize an Array

    Arrays are a central part of programming. You will use arrays of one type or another in nearly every program you create....

  • Python Lambda Tutorial

    Lambda expressions are a powerful feature in the Python programming language. However, they can be a little intimidating at first glance, especially...

Related Ads

Featured