Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8869253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:26:46+00:00 2026-06-14T17:26:46+00:00

I have this code in python. import sys list1 = [A, B, C] list2

  • 0

I have this code in python.

import sys
list1 = ["A", "B", "C"]
list2 = [1, 2, 3]

myarg = sys.argv[1]
print len(myarg)

I will run this script from command line like this

python script.py list1

So I need a way to make the len work with the list instead of the variable, I just want to give the name of the list in the var.

I know for this example I can do

if myarg == "list1":
    print len(list1)
else if myarg == "list2"
    print len(list2)

But I have 13 lists on my script and I need a way to refer to them by the name I give on the command line.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:26:47+00:00Added an answer on June 14, 2026 at 5:26 pm

    While this is entirely possible in Python, it’s not needed. The best solution here is to not do this, have a dictionary instead.

    import sys
    
    lists = {
        "list1": ["A", "B", "C"],
        "list2": [1, 2, 3],
    }
    
    myarg = sys.argv[1]
    print len(lists[myarg])
    

    You note you have 13 lists in your script – where you have lots of similar data (or data that needs to be handled in a similar way), it’s a sign you want a data structure, rather than flat variables. It will make your life a lot easier.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code: #!/usr/local/bin/python """ USAGE: apache_logs.py """ import sys import os if
I have this python code: import sys import dpkt f = file(pcaop.Pcap) pcap =
I have this code: #! /usr/bin/python import sys, string def findAll(search, fh): count =
I have this code in Python inputted = input(Enter in something: ) print(Input is
I have this code: def display(self): print self.doc.toprettyxml(indent= ) strigName ='/Users/my_user/Desktop/python/' + str(datetime.datetime.now()) +
I have a python script the runs this code: strpath = sudo svnadmin create
I have the following code: #!/usr/bin/env python import sys from PyQt4 import QtGui, QtCore
I have this code (hello.py): import os,sys import tornado.ioloop import tornado.web import tornado.httpserver #http
I have the code: import os import sys fileList = os.listdir(sys.argv[1]) for file in
I have a script containing a section similar to this, on Python 2.6: import

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.